Sliding-Window Rate Limiter
medium · sliding window
Given limit >= 1, window >= 1, and nondecreasing integer request times >= 0, return admission booleans. At time t count only ACCEPTED earlier requests in (t-window, t], then admit if the count is below limit. Remove a request exactly at t-window. Rejected requests are not recorded. Multiple requests may have the same timestamp. Use the supplied fake times only.
Examples (JSON; null means Python None):
Input: {"limit": 2, "window": 10, "requests": [0, 1, 2, 10, 11]}
Output: [true, true, false, true, true]
Input: {"limit": 1, "window": 3, "requests": [0, 0, 2, 3]}
Output: [true, false, false, true]
Starter code
def sliding_limiter(limit: int, window: int, requests: list[int]) -> list[bool]:
"""Count admitted requests in a half-open moving time window."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler