Sliding Window interview problems

Practical sliding window questions are usually about time rather than array indexes. Count requests in the last 60 seconds, alert when errors in a five-minute window cross a threshold, or keep a rolling average latency. The standard tool is a collections.deque of timestamps or (timestamp, value) pairs. You append on the right, pop from the left while the oldest entry is outside the window, and keep a running sum so each query is O(1) amortised. bisect on a sorted list works when you only need counts and the data is static.

The classic mistakes are off-by-one window bounds (is an event exactly 60 seconds old inside or outside?), recomputing the sum over the whole window on every event, and never evicting when there are no new events, so a quiet key holds memory forever. A strong answer states the boundary rule explicitly, takes the clock as a parameter so tests can control time, and discusses memory when the window holds millions of events. That leads to the follow-up of bucketing into per-second counters, which trades precision for bounded memory.

1 open problem

  1. 06Sliding Window Rate Limitermedium

    Build a per-user sliding window log rate limiter that allows at most N requests in any window of W seconds.

2 locked

These open in order of demand. Open one and press I want this one to ask for it.

  1. 24Async Sliding Window Rate Limiter Lockedmedium

    Admit at most N requests in any rolling window of T seconds, making callers await until a slot opens.

  2. 48API Key Usage Tracker Lockedmedium

    Enforce a per-API-key sliding-window quota on both request count and tokens, rejecting requests that do not fit, with memory bounded by the window.