Typorithm · medium

Rate Limiter

rate-limiterproblem-labsliding-windowthrottlingqueue
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Rate Limiter

0%

Applied Patterns

Allow at most N requests per rolling time window per user. Keep a queue of recent hit timestamps; on each call drop any older than now minus the window, then admit only if the surviving count is under the limit.

An API must allow each user at most N requests per W milliseconds — but the window rolls continuously, not in fixed buckets, so a burst at a bucket boundary cannot sneak through.

The sliding-window pattern keeps a queue of recent hit timestamps. On every call you first slide the window forward by discarding timestamps older than now minus the window length.

Whatever survives is exactly the set of requests still inside the live window. If that count is below the limit you push the new timestamp and admit; otherwise you reject.

01

Store hit timestamps in arrival order.

02

Each call prunes timestamps that fell out the back.

03

Admit only while the live count is under the limit.

class RateLimiter {
  constructor(limit, windowMs) {
    this.limit = limit;
    this.windowMs = windowMs;
    this.hits = [];
  }
  allow(now) {
    const cutoff = now - this.windowMs;
    while (this.hits.length && this.hits[0] <= cutoff) {
      this.hits.shift();
    }
    if (this.hits.length < this.limit) {
      this.hits.push(now);
      return true;
    }
    return false;
  }
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Window edge

The cutoff marks the trailing edge of the window: any hit at or before it has expired.

const cutoff = now - this.windowMs;

2. Drop stale hits

Each call slides the window forward by removing timestamps that fell out the back.

while (this.hits.length && this.hits[0] <= cutoff) {

3. Evict oldest

shift removes the oldest timestamp from the front, since hits are stored in arrival order.

this.hits.shift();

4. Capacity check

After pruning, the remaining count is exactly the requests inside the live window.

if (this.hits.length < this.limit) {

5. Admit request

Recording the new timestamp counts this request toward the next window's budget.

this.hits.push(now);

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Rate Limiter: Lesson & Practice · Typorithm | ApplyDjinn