Sliding-Window Rate Limiter Over Event Timestamps
Data Structure DesignAuto-graded · 16 test casesLast updated
Tests the ability to implement a sliding-window rate limiter that, given a log of request timestamps, determines which requests are allowed under a fixed-window capacity.
Sliding-Window Rate Limiter Over Event Timestamps
You are building the core logic for a sliding-window rate limiter.
Given a chronological log of request timestamps and a rate-limit configuration, decide which requests are allowed and which are rejected.
How the rate limiter works
- The limiter allows at most
limitrequests within any sliding window ofwindow_msmilliseconds. - Requests are processed in the order they appear in the log.
- When a request arrives at time
t, count how many previously allowed requests have a timestamp in the half-open interval(t - window_ms, t](i.e., strictly greater thant - window_msand at mostt).- If that count is strictly less than
limit, the current request is allowed (count it going forward). - Otherwise, it is rejected (do not count it going forward).
- If that count is strictly less than
- Timestamps in
timestampsare non-decreasing (requests arrive in order, ties allowed). - Each element of
timestampscorresponds to one request; process them left to right.
Your task
Implement the function:
def rate_limiter(timestamps: List[int], limit: int, window_ms: int) -> List[bool]
Return a list of booleans of the same length as timestamps, where True means the request was allowed and False means it was rejected.
Constraints
0 ≤ len(timestamps) ≤ 20000 ≤ timestamps[i] ≤ 10^9; the list is non-decreasing1 ≤ limit ≤ 20001 ≤ window_ms ≤ 10^9- If
timestampsis empty, return an empty list. - Ties (equal timestamps): each is treated as a separate request and processed in order.
Examples
Example 1
Input: rate_limiter([100, 200, 300, 400, 500], 3, 300)
Output: [True, True, True, True, True]
Example 2
Input: rate_limiter([0, 0, 0, 0], 2, 1000)
Output: [True, True, False, False]
Example 3
Input: rate_limiter([1000, 2000, 3000], 1, 500)
Output: [True, True, True]
Hints
Hint 1
Use a queue (or deque) to track the timestamps of only the allowed requests; on each new request, evict entries that fall outside the sliding window before checking the count.
Hint 2
The window is (t - window_ms, t], so evict any allowed-request timestamp that is ≤ t - window_ms.
Hint 3
You never need to store rejected timestamps — only allowed ones affect future decisions.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key insight
Only allowed requests count toward the rate limit. Rejected requests are invisible to future decisions. So we only need to track the timestamps of requests that were allowed.
Algorithm
Maintain a deque of timestamps of allowed requests (in insertion order, which is also chronological order since timestamps is non-decreasing).
For each incoming request at time t:
- Evict stale entries: pop from the front of the deque any timestamp
swheres ≤ t - window_ms(i.e.,sis outside the half-open window(t - window_ms, t]). - Check capacity: if the deque now has fewer than
limitentries, allow the request — appendTrueand pushtonto the deque. - Otherwise reject: append
Falseand do not modify the deque.
Why it is correct
- The deque always contains exactly the allowed-request timestamps that still fall inside the current window.
- Because timestamps are non-decreasing, expired entries always accumulate at the front, so a single left-to-right sweep suffices (each element is pushed and popped at most once).
- The window rule
(t - window_ms, t]is enforced precisely by evictings ≤ t - window_ms.
Complexity
| Time | O(n) — each timestamp enters and leaves the deque at most once. |
| Space | O(min(n, limit)) — the deque never holds more than limit entries. |
from typing import List
from collections import deque
def rate_limiter(timestamps: List[int], limit: int, window_ms: int) -> List[bool]:
result = []
allowed_window: deque = deque() # stores timestamps of allowed requests
for t in timestamps:
cutoff = t - window_ms
# evict allowed requests that are no longer in the window (t - window_ms, t]
while allowed_window and allowed_window[0] <= cutoff:
allowed_window.popleft()
if len(allowed_window) < limit:
result.append(True)
allowed_window.append(t)
else:
result.append(False)
return result