Sliding Window API Rate Limiter
Data Structure DesignAuto-graded · 17 test casesLast updated
Tests ability to implement a sliding-window rate limiter that counts requests within a fixed-width time window using a list of timestamps.
Sliding Window API Rate Limiter
You are building a lightweight API gateway. To protect a service, you must enforce a sliding-window rate limit: for each incoming request, decide whether it should be allowed or denied based on how many requests have already been made in the most recent window.
Task
Implement the function check_requests that, given a chronological log of request timestamps and a rate-limit configuration, returns a list of decisions ("allow" or "deny") — one per request, in order.
Rate-limit rule:
A request at time t is allowed if the number of previously allowed requests with timestamps in the half-open interval (t - window_size, t] (i.e., strictly after t - window_size and up to and including t) is strictly less than max_requests. If that count equals or exceeds max_requests, the request is denied.
Important: Denied requests do not count toward the limit — only allowed requests consume quota.
Parameters
| Name | Type | Description |
|---|---|---|
timestamps | List[int] | Arrival times of requests in non-decreasing order (seconds). |
window_size | int | Width of the sliding window in seconds (≥ 1). |
max_requests | int | Maximum allowed requests permitted inside the window (≥ 1). |
Returns
List[str] — A list of "allow" or "deny" strings, one per request, in the same order as timestamps.
Constraints
0 ≤ len(timestamps) ≤ 2000timestampsis non-decreasing:timestamps[i] ≤ timestamps[i+1]0 ≤ timestamps[i] ≤ 10^91 ≤ window_size � 10^91 ≤ max_requests ≤ 2000- If
timestampsis empty, return an empty list.
Examples
Example 1
Input: check_requests([1, 1, 1, 2, 3], 2, 2)
Output: ['allow', 'allow', 'deny', 'deny', 'allow']
Example 2
Input: check_requests([10, 20, 30, 40, 50], 15, 3)
Output: ['allow', 'allow', 'allow', 'allow', 'allow']
Example 3
Input: check_requests([], 10, 5)
Output: []
Hints
Hint 1
Keep a running list (or deque) of timestamps for requests that were allowed, and trim it as the window slides forward with each new request.
Hint 2
For each new request at time t, remove all allowed timestamps that are ≤ t - window_size before checking the count against max_requests.
Hint 3
You only add a timestamp to the allowed list when the decision is 'allow' — denied requests must be invisible to the rate limiter.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
We need to track only allowed requests within a sliding window. As each new request arrives at time t, the relevant window is (t - window_size, t]. Any allowed request at time ≤ t - window_size has "expired" and no longer occupies quota.
Algorithm
- Maintain a deque of timestamps for allowed requests (oldest at the front).
- For each incoming request at time
t:- Evict all entries from the front of the deque where
timestamp ≤ t - window_size(they are outside the window). - If the deque now has fewer than
max_requestsentries, the request is allowed and its timestamp is appended to the deque. - Otherwise the request is denied (nothing is appended).
- Evict all entries from the front of the deque where
Why It's Correct
Because timestamps are non-decreasing, expired entries always accumulate at the front of the deque — we never need to scan the middle. The deque therefore stays sorted, and each element is pushed and popped at most once, giving O(n) overall work.
Denied requests are deliberately not recorded, matching the rule that they consume no quota.
Complexity
| Time | O(n) — each timestamp enters and leaves the deque at most once |
| Space | O(min(n, max_requests)) — the deque holds at most max_requests entries at any moment |
Reference Solution
from typing import List
from collections import deque
def check_requests(timestamps: List[int], window_size: int, max_requests: int) -> List[str]:
results = []
allowed_window: deque = deque()
for t in timestamps:
cutoff = t - window_size
# Evict allowed requests outside the window (timestamp <= cutoff)
while allowed_window and allowed_window[0] <= cutoff:
allowed_window.popleft()
if len(allowed_window) < max_requests:
results.append("allow")
allowed_window.append(t)
else:
results.append("deny")
return results