KatInterview

Rate-Limited Dispatcher: Which Requests Fire and When

Data Structure DesignAuto-graded · 24 test cases
MediumAuto-gradedData Structure Designsimulationevent-driventhrottle

Last 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 time s occupies 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.

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 e of the current lockout is handled after the end-of-lockout step at e.
    • 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 at e.
  • 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^6
  • 1 <= len(request_times) <= 10^5
  • 0 <= request_times[i] <= 10^9, and request_times is 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.