March 24
Sliding Window
What sliding window means and when a moving window helps solve substring, subarray, and contiguous-range problems.
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:
- is the problem about a contiguous range?
- can I update the answer by moving only one boundary at a time?
- is there a clear rule for when the window expands or shrinks?
Share this page
Copy the link manually from the field below.