Leaky Bucket Rate Limiter
Implement a leaky bucket rate limiter with a fixed drain rate and finite capacity, supporting a `try_acquire(n)` method that returns whether a request is allowed or the wait time until it would be. Include unit tests for steady-state, burst, and edge case behavior, and discuss concurrency safety and distributed extension strategies.
Asked at:
Box
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Mid April, 2026
Box
Senior
Implement a Leaky-Bucket Rate Limiter with Tests and Distributed Design Context and Assumptions Implement in a general-purpose language; the reference solution uses Python 3 for clarity and unit tests. Leaky bucket semantics: a fixed drain rate r units/sec, a finite capacity b units. Each request consumes 1 unit unless specified otherwise. The bucket "leaks" continuously at rate r. The limiter should support: try_acquire(n=1) that returns whether a request of size n is allowed now and, if not, the wait time until it would be allowed. Unit tests for steady-state, burst, and boundary conditions. Discussion of concurrency safety (multi-thread/process) and distributed extension (e.g., Redis) with consistency & failure-mode considerations. Task Implement a leaky-bucket rate limiter enforcing a maximum average request rate with a fixed drain rate. Write unit tests to validate: Steady-state behavior at/under the configured rate. Burst behavior up to capacity. Boundary/edge conditions (precision, zero/near-zero time deltas, exact thresholds). Explain how to make the limiter safe under concurrency. Propose a design to extend it in a distributed setting (e.g., Redis), including consistency and failure-mode considerations.
Hello Interview Premium
Your account is free and you can post anonymously if you choose.