Practice ยท 2 of 2
The token bucket
Implement TokenBucket(rate, capacity) with allow(now):
- refills rate tokens per second of elapsed time, capped at capacity (first call initializes the clock without refilling)
- a request succeeds iff at least 1 full token is available; it consumes exactly 1
- now is a float timestamp โ never call time functions yourself (the bucket must be deterministic)
Burst of capacity at t=0 passes instantly; sustained throughput never exceeds rate per second.
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Boundary Drills