SOARDO
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

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).

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`.
shrinking with `if` instead of `while` on variable windows, or applying positive-only monotonic shrink when negative numbers exist!
Sliding Window Technique — GeeksforGeeksread ↗Sliding Window — Applied Practicepractice ↗VisuAlgo Interactive Visualizationsvisualize ↗NeetCode DSA Roadmappractice ↗

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.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides