KatInterview

Sliding-Window Throttle with Global, Per-User and Per-Experience Caps

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designqueuedequesliding-window

Last updated

Simulate a sliding-window throttle with a global cap and a combined per-user and per-experience cap, where the combined check must be all-or-nothing.

You are building a throttle for an API and must simulate how it responds to a sequence of calls. A single throttle instance handles every call, in order.

Operations

Two kinds of calls are supported:

  • "allow" with arguments [timestamp]. It is judged only against the global limit: at most global_limit accepted calls in any sliding window.
  • "allow_multi" with arguments [user_id, experience, timestamp]. It is judged against two independent limits:
    • at most user_limit accepted calls per user_id in any sliding window, and
    • at most experience_limit accepted calls per experience value in any sliding window.

The call is accepted only if both limits have room. If it is accepted, it counts toward both the user's tally and the experience's tally. If it is rejected, it counts toward neither. A call refused because of one limit must not consume capacity from the other.

Sliding window

The window has length window milliseconds. For a call at time t, the relevant earlier calls are the accepted ones whose timestamps fall in the closed range [t - window + 1, t]. A call is accepted if the number of such calls for the relevant key is strictly less than the limit.

Rejected calls are never counted. Several accepted calls may share the same timestamp, and all of them count.

Independence of state

  • "allow" calls and "allow_multi" calls keep separate histories. An "allow" call never affects an "allow_multi" call, and the reverse.
  • Each distinct user_id has its own history, and each distinct experience has its own history. The same user in different experiences shares one user history, and the same experience across different users shares one experience history.

Task

Implement simulate_throttle(operations, arguments, global_limit, window, user_limit, experience_limit). operations[i] is the call type and arguments[i] holds its arguments. Return a list of booleans, where entry i is True if call i was accepted and False otherwise.

Constraints

  • len(operations) == len(arguments), and each operation is either "allow" or "allow_multi".
  • Timestamps are integers and are nondecreasing across the whole sequence of calls.
  • window >= 1.
  • global_limit, user_limit and experience_limit are integers >= 0. A limit of 0 means every call it applies to is rejected.
  • user_id and experience are strings.
  • The sequence may be long, so aim for amortized constant work per call.

Examples

Example 1

Input: simulate_throttle(['allow', 'allow', 'allow', 'allow'], [[0], [100], [999], [1000]], 2, 1000, 2, 2)

Output: [True, True, False, True]

Example 2

Input: simulate_throttle(['allow_multi', 'allow_multi', 'allow_multi', 'allow_multi'], [['u1', 'e1', 0], ['u1', 'e2', 1], ['u2', 'e1', 2], ['u1', 'e1', 1000]], 10, 1000, 2, 2)

Output: [True, True, True, True]

Example 3

Input: simulate_throttle(['allow_multi', 'allow_multi', 'allow_multi'], [['u', 'game', 0], ['u', 'game', 1], ['u', 'game', 2]], 10, 100, 2, 3)

Output: [True, True, False]

Hints

Hint 1

Only accepted calls matter. Because timestamps never decrease, the accepted timestamps for a key are already sorted, so a FIFO queue per key is enough.

Hint 2

Before deciding, remove from the front of a queue every timestamp s with s <= t - window. What remains is exactly the accepted calls inside [t - window + 1, t].

Hint 3

For allow_multi, evict from both the user queue and the experience queue and run both capacity checks first. Append the timestamp to both queues only when both checks pass.

Hint 4

Keep the global history and the per-key histories completely separate, and optionally delete keys whose queues become empty to keep memory small.