Rate-Limited Dispatcher: Which Requests Fire and When
Data Structure DesignAuto-graded · 24 test casesLast updated
Simulate a rate limiter with a fixed lockout window that keeps only the newest waiting request, using event-driven processing and a precise rule for requests landing exactly at window end.
A dispatcher guards an action so that it cannot fire too often. You are given a lockout length gap and a list of request timestamps. Simulate the dispatcher and report which requests actually fire, and at what time.
Behavior
- The dispatcher starts free.
- A request that arrives while the dispatcher is free fires immediately, at its own timestamp, and starts a lockout of length
gap. A lockout that begins at timesoccupies the half-open interval[s, s + gap). - A request that arrives during a lockout does not fire right away. It becomes the waiting request and replaces any request already waiting, so only the newest one is kept.
- When a lockout ends at time
e = s + gap:- If a request is waiting, it fires at time
e, the waiting slot is emptied, and a new lockout[e, e + gap)begins. - If no request is waiting, the dispatcher becomes free.
- If a request is waiting, it fires at time
Return one pair [fire_time, request_index] for every firing, where request_index is the 0-based position in request_times of the request that fired.
Rules
- Requests are processed in input order. Several requests may share a timestamp; they are handled one after another in input order.
- Boundary rule: a request whose timestamp equals the end
eof the current lockout is handled after the end-of-lockout step ate.- If a waiting request fires at
e, the new request lands inside the fresh lockout and becomes the waiting request. - If nothing was waiting, the dispatcher is free at
e, so the new request fires immediately ate.
- If a waiting request fires at
- After the last request, keep simulating until the dispatcher is free, so a request still waiting fires when its lockout expires.
- A request that is replaced while waiting never fires and does not appear in the output.
- Return the pairs in firing order. Fire times are strictly increasing, so the order is unique.
Constraints
1 <= gap <= 10^61 <= len(request_times) <= 10^50 <= request_times[i] <= 10^9, andrequest_timesis sorted in non-decreasing order- Every fire time is at most
10^9 + 10^6
Examples
Example 1
Input: dispatch_schedule(10, [0, 3, 7, 12, 25, 30, 30])
Output: [[0, 0], [10, 2], [20, 3], [30, 4], [40, 6]]
Example 2
Input: dispatch_schedule(5, [1, 6, 6, 20])
Output: [[1, 0], [6, 1], [11, 2], [20, 3]]
Example 3
Input: dispatch_schedule(3, [0, 1, 2])
Output: [[0, 0], [3, 2]]
Hints
Hint 1
Do not step through time unit by unit; timestamps reach 10^9. The state only changes when a request arrives or a lockout ends.
Hint 2
Track just two values: when the current lockout ends (or that the dispatcher is free) and the index of the waiting request (or none).
Hint 3
Before handling a request at time ct, resolve every lockout end with end <= ct. Using <= implements the boundary rule. After the final request, run the same resolution with no upper limit.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Stepping through every time unit is too slow because timestamps reach 10^9. The state only changes at two kinds of moments: a request arrives (given in the input), or a lockout ends (always start + gap). So we simulate by events and keep two variables:
lock_end: when the current lockout ends, orNoneif the dispatcher is free.waiting: the index of the waiting request, or-1.
For each request at time ts:
- Settle lockout ends. While
lock_end <= ts: if a request is waiting, it fires atlock_end, the slot is cleared and a new lockout starts atlock_end. Otherwise the dispatcher becomes free. - Handle the request. If free, it fires at
tsand starts a lockout ending atts + gap. Otherwise it overwriteswaiting.
After the last request, run the same settling loop with no upper bound to flush a still-waiting request.
Why it is correct
- The
<=comparison implements the boundary rule: a request at exactly the lockout end sees the effect of that end first. If something was waiting it fires and the new request waits in the fresh lockout; otherwise the dispatcher is free and the request fires at once. - Overwriting
waitingmeans only the newest request survives, and replaced requests are never recorded. - Requests sharing a timestamp work naturally: the first one starts a lockout ending at
ts + gap > ts, so later ones become waiting. - Each firing happens at least
gapafter the previous one, so output is already in increasing time order. - The settling loop is short: after a waiting request fires, the slot is empty, so the next iteration (if any) just frees the dispatcher and stops.
Complexity
- Time: O(n), since each request fires at most once and each request triggers at most a constant number of settling steps (plus a short final flush).
- Space: O(1) beyond the output list.
Reference solution
from typing import List
def dispatch_schedule(gap: int, request_times: List[int]) -> List[List[int]]:
result = []
lock_end = None # end of the current lockout, or None when free
waiting = -1 # index of the waiting request, or -1 if none
for idx, ts in enumerate(request_times):
# Resolve all lockout ends at or before this request's timestamp.
while lock_end is not None and lock_end <= ts:
if waiting != -1:
result.append([lock_end, waiting])
waiting = -1
lock_end += gap
else:
lock_end = None
if lock_end is None:
result.append([ts, idx])
lock_end = ts + gap
else:
waiting = idx
# Flush: continue until the dispatcher is free.
while lock_end is not None:
if waiting != -1:
result.append([lock_end, waiting])
waiting = -1
lock_end += gap
else:
lock_end = None
return result