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 interpreter

3 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. InterviewerCharacter joke · No posting date

    Great approach. Now solve it without using the thing you just used.

  2. AntyCharacter joke · No posting date

    This problem has 8 hidden tests. I found 9 ways to fail them.

  3. The GraderCharacter joke · No posting date

    Edge case? Never met her.