← Problems62. Sliding window rate limiterMediumPython
00:00 / 30:00

Sliding window rate limiter

MediumNot attempted yetAsked at Adobe

An ingestion API throttles each key to a fixed number of calls in a trailing window. Replay a day of traffic and decide what the limiter would have done, so a proposed limit can be judged before it is turned on.

Input schema

solve(requests: list[dict], limit: int, window_seconds: int) -> list[dict]

Each request has request_id (int), api_key (str) and ts, a YYYY-MM-DD HH:MM:SS string.

Return one dict per request, in the order they were processed, with keys in this order:

request_id, decision

Constraints

  • Process in ascending ts; break ties on the same ts by ascending request_id
  • A request is allowed when the same key has fewer than limit allowed requests whose timestamp is strictly greater than ts - window_seconds
  • A denied request consumes nothing — it never counts against a later one
  • decision is ALLOW or DENY
  • A request exactly window_seconds old has left the window
  • Limits are per api_key; keys never interfere
  • Up to 10,000,000 requests

Example 1the third call in the window is refused

Input · limit
2
Input · requests
tsapi_keyrequest_id
2026-04-01 09:00:00k11
2026-04-01 09:00:10k12
2026-04-01 09:00:20k13
2026-04-01 09:01:05k14
Input · window_seconds
60
Output
request_iddecision
1ALLOW
2ALLOW
3DENY
4ALLOW

Example 2keys are limited independently

Input · limit
1
Input · requests
tsapi_keyrequest_id
2026-04-02 09:00:00k11
2026-04-02 09:00:01k22
2026-04-02 09:00:05k13
Input · window_seconds
30
Output
request_iddecision
1ALLOW
2ALLOW
3DENY

Topics

data structuresstreaming

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.