Rate Limiter
SnowflakePhone screenHard
Design a rate limiter that decides, per client, whether a request at a given time is accepted. A request accepted at time t counts against its client for every time s with t <= s < t + window; a rejected request never counts. Timestamps passed to the limiter are non-decreasing. Each test builds one limiter from the constructor arguments, runs the calls in order, and checks every return value.
Part 1: Per-client limit
Implement allow(at, client). Return true and record the request if client has fewer than default_limit accepted requests in the window ending at at; otherwise return false without recording it. Each client has its own allowance.
- Example 1
- init
[10,3]- operations
[["allow",[1,"u"]],["allow",[2,"u"]],["allow",[3,"u"]],["allow",[4,"u"]],["allow",[5,"v"]],["allow",[10,"u"]],["allow",[11,"u"]],["allow",[12,"u"]],["allow",[12,"u"]],["allow",[13,"u"]],["allow",[14,"u"]]]- Output
[true,true,true,false,true,false,true,true,false,true,false]
Why: With window 10 and limit 3, u fills its three slots at times 1-3 and is rejected at 4; v has a separate allowance. At 10 the request from time 1 still has 9 units left, so u stays full. Requests age out one per unit from 11 on, so u passes at 11, 12 and 13, but a second request at 12 and one at 14 each find three requests still counted.
Constraints
1 <= window <= 10^90 <= default_limit <= 10^50 <= at <= 10^91 <= len(client) <= 101 <= len(operations) <= 10^4atis non-decreasing across calls