9002. Token Bucket Rate Limiter

A token bucket holds at most capacity tokens and refills at rate tokens per second; it starts full. requests is a sorted list of arrival times in seconds. Each request takes one token if one is available (allowed) and is rejected otherwise. Return true/false for each request. Refill is computed lazily: on each arrival add (now - last) * rate tokens, capped at capacity. This is how API gateways and Redis-based limiters allow short bursts while holding a long-run average.

Patterns: simulation, design. Difficulty: easy.

Open in Dryrun