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
leftto indexright(both included). Its length isright - left + 1.
The problem it solves
"Find the longest substring with no repeated characters." The naive way checks every possible piece:
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
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:
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:
| Problem | Rule for a valid window | Summary you keep |
|---|---|---|
| Best Time to Buy and Sell Stock | (window = cheapest day so far → today) | the minimum price so far |
| Longest Substring Without Repeating Characters | no character appears twice | last index of each character |
| Longest Repeating Character Replacement | length - mostCommonCount <= k | letter counts + the max count |
| Minimum Window Substring | contains every character of t | need 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
kchanges", "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
leftbackward. It should only ever move forward. - Off-by-one window length. From
lefttorightinclusive isright - 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).