KatInterview

Rolling-Window Request Throttle Per Client Address

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

Last updated

Simulate a per-IP sliding-window-log throttle over a sequence of requests, using per-key queues with lazy expiry and strict half-open window boundaries.

Rolling-Window Request Throttle

A gateway protects itself by limiting how often each client address may call it. Every client is tracked separately, and the limit applies over a rolling time span rather than fixed clock buckets.

You are given the throttle's settings and a recorded sequence of calls. Replay the calls in order and report whether each one is admitted.

Rules

  • max_requests is the largest number of admitted requests a single address may have inside any rolling window.
  • window_seconds is the length of that window in seconds.
  • A request from address ip at integer time t is admitted if the number of previously admitted requests from that same address with times in the half-open interval (t - window_seconds, t] is strictly less than max_requests.
  • An admitted request is remembered for that address. A rejected request is not remembered and never counts toward any later decision.
  • An earlier admitted request made exactly window_seconds before t (that is, at time t - window_seconds) is already expired and does not count.
  • Several requests may share the same timestamp, and each is judged in the order given.
  • Different addresses are completely independent.
  • Timestamps for any one address never decrease from one call to the next.

Input

  • max_requests (int): the per-address cap.
  • window_seconds (int): the window length in seconds.
  • operations (list of str): the operation names, one per call. Every entry is "allow_request". If any other name appears, raise ValueError.
  • arguments (list of lists): arguments[i] is [ip, timestamp] for the i-th call, where ip is a string and timestamp is an integer number of seconds.

Output

Return a list of booleans with one entry per call, in order: True if the request was admitted, False otherwise.

Constraints

  • 1 <= max_requests
  • 1 <= window_seconds
  • timestamp >= 0
  • len(operations) == len(arguments), up to 10^6 calls in total.
  • For a fixed ip, timestamps are given in non-decreasing order.
  • Memory for addresses with nothing remembered should not be retained.

Examples

Example 1

Input: replay_ip_throttle(3, 10, ['allow_request', 'allow_request', 'allow_request', 'allow_request', 'allow_request', 'allow_request'], [['10.0.0.1', 1], ['10.0.0.1', 2], ['10.0.0.1', 5], ['10.0.0.1', 8], ['10.0.0.1', 11], ['10.0.0.2', 8]])

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

Example 2

Input: replay_ip_throttle(2, 5, ['allow_request', 'allow_request', 'allow_request', 'allow_request', 'allow_request'], [['a', 0], ['b', 0], ['a', 1], ['a', 5], ['b', 5]])

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

Hints

Hint 1

Only admitted requests matter for the count, so you only need to store admitted timestamps for each address.

Hint 2

Because an address's timestamps never decrease, its stored timestamps are sorted, so stale ones are always at the front. A double-ended queue per address lets you drop them cheaply.

Hint 3

Expire a stored time s when s <= t - window_seconds (not strictly less). Check the count after expiring, and append only when admitting.