Skip to the page

Frontend-flavoured problems · senior

Rate limiters: token bucket and sliding window

A rate limiter decides whether an action may happen now: a token bucket refills tokens at a fixed rate and allows bursts up to its capacity, and a sliding-window log counts timestamps in the last window. On the client, pair it with a queue so calls wait instead of failing.

The notes

What you must know

  • Fixed window counter: count per clock window (e.g. per minute); simple but allows 2× bursts at window edges.
  • Sliding window log: keep timestamps; drop those older than now − window; allow if count < limit. Exact but O(limit) memory.
  • Token bucket: tokens = min(capacity, tokens + elapsed × rate); allow if tokens >= cost, then subtract. Allows bursts, smooth average.
  • Inject the clock (now) so the limiter is testable without waiting.
  • Client-side use: protect a rate-limited API (e.g. 10 requests/min): debounce user input, dedupe, cache, then queue requests and release them when the limiter allows; honour 429 + Retry-After from the server.
  • Throttle vs rate limiter: throttle drops or delays calls to at most one per interval; a limiter counts within a window.
  • The server must enforce limits anyway; the client limiter is for user experience and to avoid wasted calls.

The red pen

Common traps: mistake → fix

  • Mistake: setInterval to refill tokens

    Fix: timers drift and keep running; compute tokens lazily from elapsed time.

  • Mistake: Fixed windows are exact

    Fix: 10 calls at 0:59 and 10 at 1:01 is 20 in two seconds.

  • Mistake: Client limiting is security

    Fix: anyone can bypass client code.

Easy to hard

Questions interviewers ask, easy to hard

  1. Implement a limiter: at most N calls per window.
  2. Fixed window vs sliding window: what is the edge problem?
  3. Implement a token bucket. Why lazy refill?
  4. Your dashboard calls an API limited to 10 requests per minute and users change filters quickly. What do you build?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview