Skip to content

Sliding Window

Contiguous range state

A sliding window is a pair of indices [left, right] that defines a contiguous range. At each step, you expand (move right) or contract (move left) to maintain an invariant, distinct characters, bounded frequency, sum ≤ target, etc. Each element enters and leaves the window at most once, so total work is O(n)O(n) even though the window size varies.

There are two flavors:

  • Variable-size window: common for “longest/shortest substring satisfying X.” Expand right; when the invariant breaks, contract left until it holds again.
  • Fixed-size window: common for “max/min over every window of size k.” Slide right forward by one and left forward by one in lockstep.

Sliding window is a state-management pattern, not just two indices. The window needs enough state to answer “is the current range valid?” That state may be a set, a frequency map, a count of matched characters, a running sum, or a monotonic deque.

How to choose the window state

Prompt shapeWindow stateTypical move
Longest substring without repeatsSet or last-seen mapShrink or jump left when a duplicate appears.
Longest range after at most k editsFrequency counts plus the best repeated countShrink when edits needed exceed k.
Permutation or anagram in a stringFixed-size frequency comparisonAdd right char, remove left char, track matches.
Minimum substring covering required charsNeed/have countersExpand until valid, shrink while still valid.
Maximum over every fixed windowMonotonic dequeDrop stale indices and weaker candidates.
Subarray sum with arbitrary signsPrefix sum + hash mapA normal window fails because sums are not monotonic.

The key test is whether moving right and left changes validity in a predictable direction. For positive numbers, a sum grows when right expands and shrinks when left contracts. With negative numbers, that monotonic behavior disappears, so prefix sums are usually safer.

Problems

  1. 121. Best Time to Buy and Sell Stock (Easy)
  2. 3. Longest Substring Without Repeating Characters (Medium)
  3. 424. Longest Repeating Character Replacement (Medium)
  4. 567. Permutation in String (Medium)
  5. 76. Minimum Window Substring (Hard)
  6. 239. Sliding Window Maximum (Hard)

Bonus problems (same pattern, outside NeetCode 150):

Key patterns unlocked here

  • Running best + single pass: Buy/Sell Stock; the “one-pass min-tracking” template.
  • Hash set / map as window state: Longest Substring Without Repeating.
  • Window + frequency count with max-freq invariant: Longest Repeating Character Replacement.
  • Anagram detection with matching counters: Permutation in String.
  • Two-counter tracking (have vs. need): Minimum Window Substring.
  • Positive-sum shrink loop: Minimum Size Subarray Sum.
  • Monotonic deque for window min/max: Sliding Window Maximum.
  • Prefix sum + hash map for subarray sum problems: Subarray Sum Equals K.

Common mistakes

  • Shrinking the window only once when it may need to shrink repeatedly.
  • Updating the best answer before the invariant is valid.
  • Treating every “subarray sum” prompt as sliding window. Negative numbers usually break the pattern.
  • Recomputing window counts from scratch. The point is incremental state.
  • Forgetting to remove stale indices from a monotonic deque.

How the problems fit together

Best Time to Buy and Sell Stock is the lightest version: a one-pass running minimum acts like a collapsed window. Longest Substring Without Repeating Characters teaches variable-size validity. Longest Repeating Character Replacement adds a less obvious invariant: window length minus max frequency is the edit count.

Permutation in String and Minimum Window Substring are frequency-map problems. The first is fixed-size. The second is variable-size and asks for the smallest valid range. Minimum Size Subarray Sum is the numeric version where positivity makes the shrink loop valid. Sliding Window Maximum shows why a window sometimes needs a second data structure. Subarray Sum Equals K is included as the contrast case: it solves a contiguous-range question, but with prefix sums rather than a traditional window.