Skip to content
← Back to Blog

The Complete Guide to Sliding Window Problems

10 min read

The Sliding Window pattern is one of the most powerful techniques for solving array and string problems in O(n) time. Instead of checking every possible subarray in O(n²), you maintain a window that slides across the data, updating state incrementally. This guide covers fixed vs variable windows, a reusable template, and five classic problems with approach sketches.

Fixed vs Variable Window

Fixed-Size Window

The window size K is given. Expand until the window reaches size K, then slide by adding one element on the right and removing one on the left. No need to shrink — the window always stays K.

Examples: Max sum of K consecutive elements, max average subarray of length K, permutation in string.

Variable-Size Window

The window size varies. Expand with the right pointer; when a constraint is violated, shrink with the left pointer until valid. Track the best answer at each valid state.

Examples: Longest substring without repeating chars, minimum window substring, longest subarray with at most K distinct.

Step-by-Step Template

Variable-Size Window Template

let left = 0;
let best = /* initial value */;

for (let right = 0; right < n; right++) {
  // 1. Expand: add arr[right] to window state
  updateState(arr[right], +1);

  // 2. Shrink while invalid
  while (windowIsInvalid()) {
    updateState(arr[left], -1);
    left++;
  }

  // 3. Update best answer (window is valid here)
  best = updateBest(best, window);
}

return best;

Fixed-Size Window Template

let windowSum = 0;
let best = -Infinity;

for (let right = 0; right < n; right++) {
  windowSum += arr[right];

  if (right >= k - 1) {
    best = Math.max(best, windowSum);
    windowSum -= arr[right - k + 1];
  }
}

return best;

5 Example Problems with Approach Sketches

1. Maximum Sum Subarray of Size K

Given an array and K, find the maximum sum of any contiguous subarray of size K. Fixed window. State: running sum. Shrink: none (slide by subtracting left element when window reaches K).

Approach: Add arr[right], when right ≥ K-1 update max and subtract arr[right-K+1].

2. Longest Substring Without Repeating Characters

Find the length of the longest substring with no repeating characters. Variable window. State: hash map of char → last index. Constraint: no duplicate. Shrink: move left past the previous occurrence of the repeating char.

Approach: If s[right] seen before, set left = max(left, lastSeen[s[right]] + 1). Update lastSeen[s[right]] = right. Best = max(best, right - left + 1).

3. Minimum Window Substring

Given s and t, find the smallest window in s containing all chars of t. Variable window. State: frequency map of t, count of chars still needed. Constraint: all chars of t matched. Shrink: when valid, move left and update best.

Approach: Expand and decrement needed count. When needed === 0, shrink from left until invalid, update minLen. Restore needed when shrinking.

4. Longest Subarray with At Most K Distinct

Find the longest subarray with at most K distinct elements. Variable window. State: hash map of element → count, distinct count. Constraint: distinct ≤ K. Shrink: when distinct > K, decrement count of arr[left], remove if 0, left++.

Approach: Add arr[right], update distinct. While distinct > K, remove arr[left] and shrink. Best = max(best, right - left + 1).

5. Permutation in String

Check if s2 contains a permutation of s1. Fixed window of size |s1|. State: frequency map of s1, matched count. Slide window of size K. Valid when matched === K.

Approach: Expand: add s2[right], update matched. When window size = K, check if matched === K. Shrink: remove s2[left], update matched.

Common Pitfalls

  • Confusing with subarray sum = K (with negatives): If the array has negatives, the window is not monotonic. Use Prefix Sum + hash map instead.
  • Maximum subarray sum (no constraint): That is Kadane's Algorithm, not sliding window.
  • Forgetting to update state when shrinking: Every element removed from the window must update your state (hash map, count, etc.).

Practice with our interactive Sliding Window animation and read How to Identify Sliding Window Problems.

Master sliding window visually

See the window expand and shrink in real time with our animated walkthrough.