Lesson 14 of 30
A plain-language introduction to the sliding window pattern, using a physical window frame you slide along a strip of numbers.
9 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Imagine a strip of paper with numbers written on it, laid out in a long row. Now imagine a small window frame — like a physical picture frame — that only shows you three numbers at a time. You hold the frame over the first three numbers, read them, then slide the frame one spot to the right. Now it shows the next three.
That's a sliding window. It's a way of looking at a small, connected chunk of a list at a time, and moving that chunk along instead of jumping around. The frame never shows you numbers that aren't next to each other — it only ever shows a continuous stretch.
The clever part isn't the frame itself. It's what you do when you slide it. Instead of adding up all three numbers fresh every single time the frame moves, you can just do two small updates: forget the number that just left the frame on the left, and add the number that just entered on the right. Everything else in the frame stayed the same, so there's no need to recheck it.
If that sounds like a small trick, it is — but it's the kind of small trick that turns a slow solution into a fast one, which is exactly what interviewers are listening for.
Given a short array of numbers, find the largest sum you can get from any 3 numbers that sit next to each other.
function maxSumOfThree(numbers) {
// Set up the window over the first 3 numbers and add them up.
let windowSum = numbers[0] + numbers[1] + numbers[2];
let biggestSoFar = windowSum;
// Slide the window one spot at a time, starting right after the first window.
for (let end = 3; end < numbers.length; end++) {
// The number entering the window is numbers[end].
// The number leaving the window is 3 spots behind it.
windowSum = windowSum + numbers[end] - numbers[end - 3];
// Keep track of the biggest sum we've seen so far.
if (windowSum > biggestSoFar) {
biggestSoFar = windowSum;
}
}
return biggestSoFar;
}
console.log(maxSumOfThree([2, 1, 5, 1, 3, 2])); // 9, from [5, 1, 3]
Trace it: the first window is [2, 1, 5], sum 8. Slide right — drop the 2, add the 1 — window is now [1, 5, 1], sum 7. Slide again — drop the 1, add the 3 — window is [5, 1, 3], sum 9, the new biggest. The frame never re-reads numbers it already knows.
end — that "3" is the window size. Getting that offset wrong is the single most common mistake here, and it's a normal one to make the first few times.This is the beginner, fixed-size version of the sliding window. The JavaScript track has a lesson on sliding window patterns that grow and shrink dynamically — like finding the longest stretch of a string with no repeated letters — and it uses this exact same subtract-one-add-one habit, just with a window that changes size instead of staying fixed at 3.