Skip to main content
Sliding Window

Fixed Size Window

The window is always exactly K elements wide. Slide it one step at a time. Use when the problem says 'subarray of size K' — e.g., max sum of K consecutive elements.

O(n)
·
O(1)

How It Works

A fixed-size sliding window maintains a running aggregate over exactly K consecutive elements. Instead of recomputing the sum of every K-length subarray from scratch (an O(n·k) approach), you compute the first window once, then slide: add the element entering on the right and subtract the element leaving on the left. Each slide is O(1), so the whole scan is O(n).

The pattern works for any invertible aggregate — sums, counts, XOR — because removing the departing element is as cheap as adding the new one. When the aggregate is not invertible (like max), you pair the window with a monotonic deque instead.

Step-by-Step Visualization

Start: Build the first window of size K=3
2
0
1
1
5
2
1
3
3
4
2
5
Window [0..2]
Window Sum8
Max Sum8
1/5

Code

Java
static int maxSumSubarray(int[] nums, int k) {
  // Step 1: Sum the first window
  int windowSum = 0;
  for (int i = 0; i < k; i++) {
    windowSum += nums[i];
  }
  int maxSum = windowSum;

  // Step 2: Slide the window
  for (int i = k; i < nums.length; i++) {
    windowSum += nums[i] - nums[i - k]; // Add new, remove old
    maxSum = Math.max(maxSum, windowSum);
  }

  return maxSum;
}

// Example: maxSumSubarray(new int[]{2, 1, 5, 1, 3, 2}, 3)
// Answer: 9 (subarray [5, 1, 3])

Tips & Gotchas

1Always initialize the window sum with the first K elements
2Slide by adding the new right element and removing the old left element
3Works for any associative operation: sum, product, count, XOR

Practice Problems

  • 1Maximum Sum Subarray of Size K
  • 2Maximum Average Subarray I
  • 3Find All Anagrams in a String
  • 4Sliding Window Maximum

About the Sliding Window Pattern

Instead of recalculating from scratch for every subarray, keep a 'window' that slides across the array. As the window moves right, add the new element and remove the old one. This turns O(n·k) brute force into O(n).

Key insight

When you see 'subarray', 'contiguous', or 'in-place', think arrays. The key is reducing brute-force O(n²) to O(n) using sliding window, two pointers, or prefix sums.

Common Array Interview Problems

  • Two Sum
  • Best Time to Buy & Sell Stock
  • Maximum Subarray
  • Merge Intervals
  • Product of Array Except Self
  • Container With Most Water

Frequently Asked Questions

When should I use a fixed-size window instead of a variable-size window?

Use a fixed-size window when the problem specifies an exact subarray length, such as 'size K' or 'exactly K consecutive elements'. If the window must grow or shrink to satisfy a condition (like 'at most K distinct characters'), use a variable-size window instead.

What is the time and space complexity of the fixed sliding window?

One pass over the array gives O(n) time. Space is O(1) for aggregates like sums; it grows to O(k) only when you must track window contents, such as character counts in a frequency map.