Q&A-1Explain the sliding-window idea and how it changes a production decision.
Answer
The 90-second version. A sliding window keeps a summary of one contiguous range and moves its two boundaries one step at a time, updating the summary incrementally instead of rebuilding it. The production decision it changes is whether you can afford to evaluate a rule per request inline: incremental update makes each boundary move O(1), so a per-request check costs the same whether the window holds 10 events or 10,000. That is the difference between a rate limiter that runs at the edge and one that has to be precomputed in batches.
Mechanism. Extending right adds one element's contribution; shrinking left removes one. Both are O(1) if the summary supports add and remove.
# O(1) per boundary move
total += a[right]
total -= a[left]
# Same shape, quadratic cost: rebuilds the window on every move
total = sum(a[left:right + 1])At n = 100,000 with a window of 1,000, the first is 100,000 operations; the second is ~100 million. (Worked example.)
Where it decides a production choice. Rate limiting is the case interviewers reach for, and the alternatives differ on memory and boundary accuracy:
| Approach | Per-key state | Boundary behaviour | Reach for it when |
|---|---|---|---|
| Fixed window counter | 2 ints | allows up to 2× the limit across the boundary | abuse protection, memory-tight |
| Sliding window counter | 2 ints + window start | blends the current window with the fraction of the previous one still inside the range; worst-case error is one previous window's worth of requests | default for most public APIs |
| Sliding window log | one timestamp per request | exact | billing, contractual limits |
Worked figures: a 100 req/min limit with 1M active keys. The log stores up to 100 timestamps × 8 bytes ≈ 800 B/key → ~800 MB. The counter stores ~16 B/key → ~16 MB. That 50× gap is the deciding criterion when the limit is advisory; when it is contractual, you pay the memory for exactness.
When not to use it. A sliding window needs a summary that supports removal. Sums, counts and rates do. Max and min do not: removing the maximum requires knowing the next-largest, so you keep a monotonic deque instead. Trace for window size 3 over [1, 3, -1, -3, 5, 3, 6, 7]:
| i | value | deque (indices) after push | window max |
|---|---|---|---|
| 0 | 1 | [0] | – |
| 1 | 3 | [1] | – |
| 2 | -1 | [1, 2] | 3 |
| 3 | -3 | [1, 2, 3] | 3 |
| 4 | 5 | [4] | 5 |
| 5 | 3 | [4, 5] | 5 |
| 6 | 6 | [6] | 6 |
| 7 | 7 | [7] | 7 |
Maxima: 3, 3, 5, 5, 6, 7. The deque gives O(1) amortised max, but it only supports removal from the ends: you cannot evict an arbitrary element, so it does not generalise to a window whose boundaries move out of order. Prefix sums are the other alternative: O(n) precompute, O(1) for any range query, but they need the whole array resident and an invertible summary, so they do not work on a stream.
Failure modes. Recomputing the summary inside the loop restores the quadratic cost while the code still looks like a sliding window; small inputs and short windows hide it. Detect it by counting operations per element or profiling, if per-element cost grows with window size, the summary is being rebuilt. In the deque version, forgetting to evict the front index when it falls out of the window returns a stale maximum; assert the front index is always ≥ right − k + 1.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.