Skip to main content

Sliding Window

What sliding window means and when a moving window helps solve substring, subarray, and contiguous-range problems.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

What it is

Sliding window is a technique where you maintain a contiguous range of the input and adjust its boundaries as the problem changes.

Instead of recalculating everything for each segment, you carry state from one window to the next.

When to use it

It often fits when the problem talks about:

  • substring
  • subarray
  • contiguous sequence
  • character or sum limits

The strongest signal is: the range matters, and moving one boundary at a time reuses work.

Common mistake

The classic mistake is calling every two-index problem a sliding window problem.

If the window is not maintaining a clear property as it moves, the name alone does not help.

Better question

Before applying it, ask:

  1. is the problem about a contiguous range?
  2. can I update the answer by moving only one boundary at a time?
  3. is there a clear rule for when the window expands or shrinks?

Keep exploring