Show the largest sum of three consecutive values.
Acceptance criteria
- 1.#out should show best 9.
DSA Tutorial · Two Pointers and Sliding Window
These 10 tasks go with The Sliding Window Technique chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “If a problem asks for the longest or shortest contiguous run that satisfies something, it is a sliding window. That single recognition saves more time than any other pattern here.” .
1.Best fixed windowexercise · easy · 10 min
Show the largest sum of three consecutive values.
Passes when: #out should show best 9.
2.Longest unique runexercise · easy · 10 min · practice plan
Show the length of the longest run with no repeats.
Passes when: #out should show run 3.
3.Window averageexercise · easy · 10 min · practice plan
Show the average of the first two values as a whole number.
Passes when: #out should show avg 3.
4.Shrink until validexercise · easy · 10 min · practice plan
Find the shortest run summing to at least 7 and show its length.
Passes when: #out should show length 2.
5.Count windowsexercise · medium · 10 min · practice plan
Count how many windows of size two exist in a four-element array.
Passes when: #out should show three windows.
6.Fix the window recomputed each timebug fix · easy · 8 min
Re-adding the whole window makes the pass quadratic and doubles the total.
Passes when: #out should show best 9.
7.Fix the window that never shrinksbug fix · medium · 8 min · practice plan
Without removing from the left, the run keeps growing past a repeat.
Passes when: #out should show run 3.
8.Fix the shrink loop using ifbug fix · easy · 8 min · practice plan
A single if only removes one element, so the window stays too wide.
Passes when: #out should show length 2.
9.Fix the window countbug fix · easy · 8 min · practice plan
The number of windows is length minus size plus one.
Passes when: #out should show three windows.
10.Fix the window applied to negativesbug fix · easy · 8 min · practice plan
With negative values a growing window can improve, so a window is the wrong tool.
Passes when: #out should show use prefix sums.
Between them these tasks cover Fixed windows, Variable windows and Running totals.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.