Simulate Rate Limiter Decisions
Data Structure DesignAuto-graded · 19 test casesLast updated
Simulate a thread-safe fixed-window rate limiter processing a sequence of request timestamps.
You are building a simulation of a rate-limiting mechanism to evaluate how request calls are accepted or rejected over time under safe concurrent execution semantics.
The rate limiter enforces a capacity limit of limit requests per 1.0-second fixed time window:
- The very first request timestamp sets the initial window start time (
window_start). - For any incoming request at timestamp
t, ift - window_start >= 1.0, a new 1.0-second window begins immediately with its start time set tot, and the window count resets. - If the current window count is strictly less than
limit, the request is accepted (True), and the window count increments by 1. - Otherwise, the request is denied (
False).
Given an integer limit and a list of float timestamps sorted in non-decreasing order representing arrival times of incoming requests, return a list of booleans indicating whether each corresponding request is accepted (True) or rejected (False).
Constraints
0 <= limit <= 10^50 <= len(timestamps) <= 10^5- Timestamps are non-decreasing floats:
timestamps[i] <= timestamps[i+1]for all validi.
Examples
Example 1
Input: rate_limiter_decisions(3, [0, 0.1, 0.2, 0.9, 1, 1.1])
Output: [True, True, True, False, True, True]
Example 2
Input: rate_limiter_decisions(2, [10, 10, 10.2, 10.999, 11])
Output: [True, True, False, False, True]
Example 3
Input: rate_limiter_decisions(1, [5.25, 6.25, 6.26])
Output: [True, True, False]
Example 4
Input: rate_limiter_decisions(0, [0, 0.5, 1, 2])
Output: [False, False, False, False]
Hints
Hint 1
If the timestamps list is empty, immediately return an empty list.
Hint 2
Track two state variables while iterating: the start time of the active 1-second window and the number of requests accepted in that window.
Hint 3
Check if `t - window_start >= 1.0`. If true, update `window_start = t` and reset the count to 0 before deciding whether to accept the request.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can model the rate limiter deterministically by processing the sorted timestamps sequentially:
- State Tracking: Maintain
window_start(the start time of the current 1.0-second window) andused(the count of accepted requests within this window). - Window Reset: For each timestamp
t, check ift - window_start >= 1.0. If so, a full 1.0 second or more has passed since the current window began. Resetwindow_start = tand setused = 0. - Decision & Increment: If
used < limit, approve the request (True) and incrementused. Ifused >= limit, reject the request (False).
Complexity Analysis
- Time Complexity: $\mathcal{O}(N)$ where $N$ is the number of timestamps, as we process each timestamp in a single pass with constant time operations.
- Space Complexity: $\mathcal{O}(N)$ to store and return the list of boolean decision results.
Solution
from typing import List
def rate_limiter_decisions(limit: int, timestamps: List[float]) -> List[bool]:
if not timestamps:
return []
window_start = timestamps[0]
used = 0
result = []
for now in timestamps:
if now - window_start >= 1.0:
window_start = now
used = 0
if used < limit:
used += 1
result.append(True)
else:
result.append(False)
return result