Skip to lesson
Exit
The Interview Loop, Stage by Stage1 / 2

1 min lesson

Token-bucket evidence

Talk through the example in "Token-bucket evidence", then name the result it is meant to produce.

Step 1 of 2

Token-bucket evidence

Explain that each key owns a bucket capped at burst, elapsed time refills tokens at ratePerSec, each allowed request consumes one token, an empty bucket rejects, and tests cover burst exhaustion plus refill boundaries; if uncertain, reason it through instead of inventing behavior.

A token-bucket rate limiter practice example, written cleanly under a timeboxts
type Bucket = { tokens: number; last: number };

function makeLimiter(ratePerSec: number, burst: number) {
  const buckets = new Map<string, Bucket>();
  return function allow(key: string, now = Date.now()): boolean {
    const b = buckets.get(key) ?? { tokens: burst, last: now };
    const refill = ((now - b.last) / 1000) * ratePerSec;
    b.tokens = Math.min(burst, b.tokens + refill);
    b.last = now;
    if (b.tokens < 1) { buckets.set(key, b); return false; }
    b.tokens -= 1;
    buckets.set(key, b);
    return true;
  };
}
Practice move

In this optional rate-limiter drill, explain that each key has a bucket capped at burst, elapsed time refills tokens at ratePerSec, an allowed request consumes one token and an empty bucket rejects. Then test burst exhaustion and time-based refill boundaries. The posting names rate limiting as example infrastructure work, not as an interview question.