Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Sliding Window

Two pointers that bound a contiguous range: grow and shrink a window, with a hash map counting what is inside.

A sliding window is a range [left, right] of neighbouring items in an array or string that slides forward, growing on the right and shrinking on the left. It's the go-to tool whenever a problem asks about the best contiguous piece: the longest substring with some property, the shortest subarray that covers something, and so on.

Words you'll see

  • Contiguous / substring / subarray: a piece made of items that sit next to each other, with nothing skipped. In "abcde", "bcd" is a substring; "bd" is not.
  • Window: the piece you're currently looking at, from index left to index right (both included). Its length is right - left + 1.

The problem it solves

"Find the longest substring with no repeated characters." The naive way checks every possible piece:

js
for (let left = 0; left < n; left++) {
  for (let right = left; right < n; right++) {
    // is s[left..right] valid?
  }
}

That's about n²/2 pieces, O(n²), and each check may cost more on top.

But neighbouring pieces overlap almost completely. "abc" and "abcd" differ by one letter. A sliding window reuses what it knows about the current piece and only updates for the one item that enters or leaves.

The picture

Loading diagram...

Here the window is "bca" (from left to right). Moving right adds "b" on the right. Moving left drops "b" on the left. Each move changes the window by exactly one item.

The template

Almost every sliding window solution has this shape:

js
let left = 0
let best = 0
// whatever you need to describe the window: a Set, a count Map, a sum...
for (let right = 0; right < s.length; right++) {
  // 1. ADD s[right] to the window

  // 2. While the window breaks the rule: REMOVE s[left] and move left forward
  while (/* window is invalid */) {
    // remove s[left]
    left++
  }

  // 3. The window is valid now: RECORD its length (or the window itself)
  best = Math.max(best, right - left + 1)
}

Why is this O(n) if there's a loop inside a loop? Because left only ever moves forward. Across the whole run, right moves n times and left moves at most n times. That's at most 2n steps in total.

What you keep "inside" the window

To check the rule in O(1), keep a small summary of what's in the window, and update it as items enter and leave:

ProblemRule for a valid windowSummary you keep
Best Time to Buy and Sell Stock(window = cheapest day so far → today)the minimum price so far
Longest Substring Without Repeating Charactersno character appears twicelast index of each character
Longest Repeating Character Replacementlength - mostCommonCount <= kletter counts + the max count
Minimum Window Substringcontains every character of tneed counts + a missing number

This is where the hash map from Arrays & Hashing comes back: it's the window's memory.

Longest vs. shortest

  • Longest valid window: grow on the right; shrink only when the window breaks the rule; record after shrinking.
  • Shortest valid window: grow until the window becomes valid; then shrink while it stays valid, recording at every step.

How to recognize this category

Think "sliding window" when the problem:

  • asks for the longest or shortest substring / subarray with some property
  • says contiguous or consecutive
  • involves a running condition like "at most k changes", "no repeats", "contains all of these"
  • and the brute force would try every start and end position

Common mistakes

  • Forgetting to remove the item that leaves on the left, so the summary drifts out of date.
  • Moving left backward. It should only ever move forward.
  • Off-by-one window length. From left to right inclusive is right - left + 1.
  • Using a sliding window when the piece doesn't have to be contiguous. If items can be picked from anywhere, it's a different problem (often hashing or dynamic programming).

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.