Sliding Window Max Sum of K
Step by Step
Every move adds the number that enters the window and subtracts the number that leaves.
Move the window back and forth, then share a link to the step where the total looked wrong.
What You Watch While the Window Slides
The window and its live total change on every step.
Every move changes only two numbers in the total.
CodeStory's visualiser shows the window growing and shrinking with the live total inside it. Highlighted code lines follow along, and you can step back, step forward or play.
Start with a small k. Then open Wide window to compare brute force with the sliding version.
How the Window Slides Along the Array
Steps below follow the window on your screen.
- 1Add up the first k numbers to get the first window sum.
- 2Add the next number, which enters on the right.
- 3Subtract the number that leaves on the left.
- 4Keep the larger of the new sum and the top sum so far.
- 5Stop when the window reaches the last number.
Where Brute Force Goes Slow
The switch lives in the visualiser. Flip it on and watch the additions grow.
- Brute forceAdds all k numbers again for every window, so you get about n × k additions instead of about 2n. Load Wide window to see the gap.
Window Inputs Worth Trying
Six presets cover the edge cases that break sliding windows.
- k = 1Every window is one number, so the answer is the maximum. Sliding saves nothing here.
- k = nOnly one window fits, so the window never slides.
- All negativeEvery sum is below 0, so the top sum starts from the first window and never from 0.
- k > nNo window fits at all.
- Every window tiesOnly a bigger sum replaces the top sum, so the first window stays.
- Wide windowSwitch on brute force here too and watch it add 6 numbers again for every window.
Sliding Window Cost in Time and Space
Interviewers ask why sliding beats brute force, and the visualiser counts real operations.
- Time
- O(n)counted from the additions and subtractions you step through
- Space
- O(1)extra memory is a few variables
- Pattern
- Sliding windowAdd the new number and drop the old one.
Practise Sliding Window Beyond Max Sum
Watching shows the window and solving trains you to size it.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
Sign up for 100 AI credits now and get 100 more every month. Start free in the app
Sliding Window Visualiser FAQs
Open the visualiser and follow the window step by step.
Open the visualiserWhat does the sliding window visualiser show?
CodeStory's visualiser shows the window growing and shrinking with its live total, plus the highlighted line of code beside it. You can move one step at a time in either direction or play it.
What is the time complexity of max sum of k in a row?
O(n) time and O(1) space. CodeStory's visualiser counts real operations while it plays, so you watch the additions stay near 2n instead of trusting a formula.
What is the sliding window technique?
Sliding window keeps a range of the array and moves its edges instead of starting over. Here the window holds k numbers, so every move adds one number and subtracts one.
Why is brute force slower than a sliding window?
Brute force adds all k numbers again for every window, which costs about n × k additions. Sliding adds one number and subtracts one, so the cost stays near 2n.
Can I use my own input?
Yes. Enter your own array and a value for k, and the visualiser slides the window across it.
Is the visualiser free?
Yes. You get 100 AI credits at sign up and 100 more every month, and watching an algorithm spends none.
Stuck on Sliding Window? Get a Free Callback
Tell CodeStory which window problem loses you, and a mentor plans what comes next.
- 1CodeStory checks your level, from window basics to interview ready.
- 2CodeStory explains online and campus batches, with timings that fit your week.
- 3You decide. No pressure, no payment on the call.