The Velvet Rope
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
Problem
A request gateway throttles each client independently: within any window of `period_seconds`, one client may have at most `max_calls` requests admitted. Build a `RateLimiter(max_calls, period_seconds)` whose `allow(user_id, now)` admits a request only when that client has fewer than `max_calls` admitted requests still inside the trailing window that ends at `now` and reaches back `period_seconds`; a request that landed exactly `period_seconds` ago has already aged out, and a rejected request is never recorded against the client. The harness replays the calls through `RateLimiter_driver(label, max_calls, period_seconds, calls)`, which builds one limiter and feeds each `[user_id, timestamp]` pair from `calls` to `allow` in order, returning the list of decisions.
Summary
Some users get in. Others wait outside until the window resets.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.