KatInterview

Replay Requests Through a Per-Key Throttle

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designsliding-windowdequehash-map

Last updated

Simulate a per-key sliding-window throttle over a batch of timed requests, tracking only accepted requests and handling window-boundary expiry correctly.

Per-Key Request Throttle Replay

A service throttles traffic separately for each client identifier. You are given the throttle's configuration and a chronological log of calls. Your job is to replay the log and report whether each call was accepted.

Rules

  • The throttle is configured with max_requests, the largest number of accepted requests a single key may have inside its window, and window_size_ms, the window length in milliseconds.
  • Every entry in operations is the string "allow". The entry at the same index in arguments is a two-element list [key, timestamp], holding the client key and the time of the call in milliseconds.
  • When a call arrives for key at time t, only that key's earlier accepted requests are considered. An earlier accepted request at time s is still active if s > t - window_size_ms. In other words, the active range is the half-open interval (t - window_size_ms, t]. A request made exactly window_size_ms milliseconds earlier has already expired.
  • If the number of active accepted requests for that key is strictly less than max_requests, the call is accepted (result True) and counts toward that key's future windows. Otherwise it is rejected (result False).
  • Rejected calls are not recorded and do not use up any quota.
  • Different keys never influence each other.
  • Several calls may share the same timestamp, and each one is judged in log order. Timestamps never decrease across the log.

Task

Return a list of booleans, one per call, in the same order as the input.

If max_requests is 0, every call is rejected.

Constraints

  • operations and arguments have the same length.
  • Every operation is "allow".
  • Timestamps are integers and are non-decreasing across the whole log.
  • max_requests >= 0 and window_size_ms >= 1.
  • Aim for amortized constant time per call, not a rescan of the whole history on every call.

Examples

Example 1

Input: replay_throttle(2, 1000, ['allow', 'allow', 'allow', 'allow'], [['user1', 100], ['user1', 500], ['user1', 800], ['user1', 1200]])

Output: [True, True, False, True]

Example 2

Input: replay_throttle(2, 1000, ['allow', 'allow', 'allow', 'allow', 'allow'], [['a', 0], ['b', 0], ['a', 1000], ['b', 1000], ['a', 1001]])

Output: [True, True, True, True, True]

Example 3

Input: replay_throttle(1, 100, ['allow', 'allow', 'allow'], [['x', 10], ['x', 10], ['x', 110]])

Output: [True, False, True]

Hints

Hint 1

Keep a separate queue of accepted timestamps for each key, oldest at the front.

Hint 2

Before deciding, drop timestamps from the front while they are less than or equal to t - window_size_ms. A gap of exactly the window size means the old request has expired.

Hint 3

Only append the current timestamp when the call is accepted. Because timestamps never decrease, the queue stays sorted and each entry is added and removed at most once.