call(back)
Algorithms & Data StructuresmediumSign in to star problems

Dropped Requests Under Two Rules

Requests arrive at non-decreasing times (in milliseconds). Each rule [window, limit] says: at most limit requests may be accepted in any window of window ms. A request at time t is accepted when, for every rule, fewer than limit accepted requests fall in (t - window, t]; otherwise it is dropped.

Return the times of the dropped requests, in order. Dropped requests do not count toward any window.

droppedRequests([100, 200, 300, 400, 5000], [[1000, 3], [10000, 20]])  ->  [400]

Hints

Hint 1

Keep one queue of accepted timestamps per rule. For each request, evict timestamps at or before t - window from the front, then check every queue's length against its limit.

Hint 2

Only on acceptance do you append the timestamp — to every rule's queue. Appending a dropped request is the classic bug and makes later requests fail that should pass.

Asked at