2026-02-20 · 8 min read

Why Sliding Window Works

The invariant that makes sliding window correct and O(n).

Why Sliding Window Works

A sliding window works when the feasibility of a range is monotonic as you expand or shrink.

Template

Maintain left/right pointers and a structure that updates in O(1) or O(log n).

See Longest Substring Without Repeating Characters.