Software Engineer (SDE)dsaintermediate3 interactive MCQs in app
Sliding Window
fixed-size vs variable-size windowamortized O(n) forward-only pointerswhy negative numbers break monotonic variable window
Core Concept
Sliding window maintains a contiguous range `[left..right]` dynamically, reusing prior window state in O(n) total time instead of O(n * k) or O(n^2) nested loops. Two flavors: (1) Fixed window size `k` (add `nums[right]`, subtract `nums[right - k]`); (2) Variable window (expand `right` until invalid/valid, shrink `left` while preserving invariant). Because `left` and `right` only move forward, each element enters and leaves at most once -> O(n).
Worked Example
for fixed size `k=3` on `[2, 1, 5, 1, 3, 2]`, start sum `8` (`2+1+5`), then slide in O(1) per step: `8 - 2 + 1 = 7`, `7 - 1 + 3 = 9` (best!), `9 - 5 + 2 = 6`.
Common Pitfall
shrinking with `if` instead of `while` on variable windows, or applying positive-only monotonic shrink when negative numbers exist!
Recommended Readings & Practice
Practice Sliding Window with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.