KatInterview

Dual-Quota Request Throttle with Sliding Time Window

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

Last updated

Simulate a throttle that enforces a per-user and a per-game quota over a sliding time window, recording a request only when both quotas have room.

Dual-Quota Request Throttle

A service receives a stream of requests, each made by a user against a game and stamped with a time in milliseconds. You must decide, for each request in order, whether to accept or reject it. Two independent quotas apply:

  • User quota: a single user may have at most per_user_cap accepted requests inside the window.
  • Game quota: a single game may have at most per_game_cap accepted requests inside the window.

The window

The window has length window_ms. When a request arrives at time t, an earlier accepted request at time s counts toward the quota only if t - s < window_ms. In other words, the window is (t - window_ms, t]. A request made exactly window_ms milliseconds earlier no longer counts.

Decision rule

For the request (user_id, game_id, t):

  1. Count the accepted requests of that user inside the window.
  2. Count the accepted requests of that game inside the window.
  3. Accept the request only if the user count is strictly less than per_user_cap and the game count is strictly less than per_game_cap.
  4. If accepted, record t for both the user and the game. If rejected, record nothing for either. A rejected request never uses up any quota.

Only accepted requests are ever counted. Several requests with the same timestamp each count separately.

Input format

  • operations: a list of strings. Every entry is "allow".
  • arguments: a list of the same length. The i-th entry is [user_id, game_id, timestamp_ms] for the i-th operation.
  • per_user_cap: the user quota.
  • per_game_cap: the game quota.
  • window_ms: the window length in milliseconds.

If any operation is not "allow", or any argument list does not have exactly three integers, raise ValueError.

Output

Return a list of booleans, one per request in input order. Each is True if the request was accepted and False otherwise.

Constraints

  • operations and arguments have the same length.
  • Timestamps are non-decreasing across the stream.
  • Timestamps, ids, and window_ms are integers. window_ms >= 1.
  • per_user_cap and per_game_cap are non-negative integers. A cap of 0 rejects every request.
  • The number of requests can be large, so aim for roughly linear total time.

Examples

Example 1

Input: throttle_requests(['allow', 'allow', 'allow', 'allow', 'allow'], [[1, 10, 1000], [1, 10, 1001], [2, 10, 1002], [1, 20, 1003], [1, 10, 2000]], 2, 2, 1000)

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

Example 2

Input: throttle_requests(['allow', 'allow', 'allow'], [[7, 3, 0], [7, 4, 1], [8, 3, 2]], 1, 1, 10)

Output: [True, False, False]

Hints

Hint 1

Only accepted requests matter, so keep a log of accepted timestamps per user and per game. Because time never goes backwards, each log is already sorted.

Hint 2

Before deciding, drop timestamps from the front of both logs that satisfy t - s >= window_ms. A double-ended queue makes this cheap.

Hint 3

Check both quotas before modifying anything. Append the timestamp to both logs only if both have room, otherwise leave both untouched.