Token Bucket Rate Limiter Class
Problem
Implement TokenBucket(capacity, refill_rate, clock=time.monotonic), a rate limiter shared by many threads:
- The bucket holds at most
capacitytokens and starts full. - Tokens are added continuously at
refill_ratetokens per second, never exceedingcapacity. try_acquire(n=1) -> bool: if at leastntokens are available, remove them and returnTrue; otherwise returnFalseimmediately without removing anything.acquire(n=1, timeout=None) -> bool: liketry_acquire, but if tokens are not available, sleep until they will be (or untiltimeoutelapses) and then take them. Must not busy-wait.
Guarantees: under any interleaving of threads, the number of successful acquisitions over any window never exceeds capacity + refill_rate × window (no over-admission), and no thread that is waiting is woken more often than needed. The clock must be injectable so tests can control time deterministically.
capacity >= 1,refill_rate > 0,n <= capacity(larger requests are rejected outright).- Refill must be lazy (computed on access), not driven by a background thread.
Examples
TokenBucket(capacity=5, refill_rate=1)at t=0: fivetry_acquire()calls succeed, the sixth fails. At t=2.0, two more succeed and the next fails (2 tokens refilled, both spent).- Same bucket, idle from t=2 to t=100 → at t=100 only 5 tokens are available, not 98: the bucket caps the burst.
- 8 threads each hammer
try_acquire()1,000 times against a bucket with capacity 100 and a frozen clock → exactly 100 succeed in total, never 101. acquire(3)on an empty bucket withrefill_rate=10→ returnsTrueafter ~0.3 s, having slept, not spun.
Hints
- You do not need a timer to add tokens. Whenever someone touches the bucket, compute how much time has passed since the last touch and add
elapsed × ratetokens, capped at capacity. - The refill-then-check-then-subtract sequence is a read-modify-write on shared state. Two threads doing it without a lock can both see "1 token left" and both take it.
- For the blocking version, compute exactly how long until
ntokens will exist, and sleep for that long outside the lock so you do not block other callers.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.