Rate Limiter

Asked at SnowflakePhone screen

Reported by the problem's creator, not verified

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.

Examples

  1. Example 1

    Input: 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]

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.