Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Per-User Rate Limiter

Model independent user limits and implement a thread-safe token-bucket decision core.

For this exercise, assume each user has an immutable token-bucket policy with capacity and refill rate.

Implement the in-process decision core behind allow(userId, cost, now): users consume independent buckets, time is supplied by a monotonic clock, a decision returns allowed plus remaining tokens and retry-after information, and policy changes take effect through an explicit version.

Concurrent calls for one user must never spend the same token twice, while unrelated users should not share one global lock. Design the model and interfaces, then implement the bounded core with deterministic tests. Exclude distributed coordination and persistence.