KatInterview

Per-User Request Throttle Simulator with Rolling Time Window

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

Last updated

Replay a list of throttle operations and decide, per user, whether each request fits within k accepted requests in a rolling window of t ms, using inclusive boundary rules.

Per-User Request Throttle

A service limits how often each user may call it. At most k requests from the same user may be accepted within any rolling window of t milliseconds. You are given a recorded sequence of operations to replay, and you must report the outcome of each one.

Input

  • operations: a list of strings. Each one is either a setup operation or a request operation.
    • Setup: "RateLimiter" (the names "__init__" and "init" mean the same thing).
    • Request: "allow_request".
  • arguments: a list of the same length as operations. arguments[i] holds the arguments for operations[i].
    • Setup: [k, t], the maximum number of accepted requests and the window length in milliseconds.
    • Request: [user_id, timestamp], a string user identifier and an integer timestamp in milliseconds.

Behavior

  • A setup operation configures k and t and discards every previously stored request, for all users. Its result is None.
  • For a request [user_id, timestamp], look only at the accepted requests previously made by that same user. Users never affect each other.
    • An earlier accepted request at time s counts toward the current window when s >= timestamp - t. The window is closed at both ends: [timestamp - t, timestamp].
    • If fewer than k earlier accepted requests count, the new request is accepted. The result is True, and the request is remembered for later calls.
    • Otherwise the request is rejected. The result is False, and the request is not remembered, so rejected requests never use up capacity.
  • Any other operation name is invalid, and the function should raise ValueError.

Output

Return a list with one entry per operation, in order. Use None for setup operations and True or False for request operations.

Constraints

  • operations and arguments have the same length, and the first operation is a setup operation.
  • k and t are positive integers.
  • Every timestamp is a positive integer.
  • Within the calls for a given user, timestamps are strictly increasing.
  • Old entries should be discarded efficiently, so the memory kept for each user stays bounded by k.

Examples

Example 1

Input: simulate_request_throttle(['RateLimiter', 'allow_request', 'allow_request', 'allow_request', 'allow_request'], [[2, 1000], ['user1', 100], ['user1', 500], ['user1', 1200], ['user1', 1300]])

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

Example 2

Input: simulate_request_throttle(['RateLimiter', 'allow_request', 'allow_request', 'allow_request'], [[1, 100], ['u', 100], ['u', 200], ['u', 201]])

Output: [None, True, False, True]

Hints

Hint 1

Keep a separate queue of accepted timestamps for each user, for example in a dictionary from user id to a deque.

Hint 2

Because timestamps only grow, the entries that have expired are always at the front of the queue. Pop from the front while the oldest entry is strictly less than timestamp - t.

Hint 3

Check the boundary carefully: an entry exactly t milliseconds old still counts. Only append the timestamp when the request is accepted, never when it is rejected.