Interview: Two Pointers & Sliding Window
Recognition first, mechanics second
Most material teaches the two techniques and leaves recognition to luck, which is why people memorise solutions and freeze on a variant. This starts at the decision instead: given an unlabelled problem, does a window apply at all, which of the three motions does it want, and what breaks when the values can be negative.
- Modules
- 4
- Drills
- 7
- Build steps
- 4
- Time
- ~3h
Walk out able to
Take an unseen array problem, say in ten seconds whether a window applies and which shape it wants, and write the loop with its edges right first time.
What this covers
The three motions
Closing in, trailing, and a window between two edges.
Does it apply at all?
Two questions that settle it before any code is written.
What negatives break
Not the window — the shrink. And what replaces it.
Edges and empty input
The six sums, and the calls that answer instead of failing.
The pitch
What you practise, and what you leave with
A fixed window completed from a skeleton, a variable window written from a specification, and a problem that must be refused before it can be solved.
You will practise
- Which of the three motions a problem wants
- What negatives do, and what they do not
- Reading a nearly-right shrink for its one fault
- Edges, widths, and degenerate input
Afterwards you can
- Decide in ten seconds whether a window applies
- Name the precondition a loop is leaning on
- Write either template with its edges right
- Refuse a problem, and say precisely why
Modules
4 modules, 19 items
Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.
- 1
Three shapes, one idea
~39 minTell the three pointer motions apart, and walk any of them to its stopping point.
- LessonWhy recognition, not templates4 min
- LessonTell the three motions apart8 min
- LessonWhat each motion is leaning on6 min
- DrillClose in from both ends11 min
- DrillWalk the writer forward10 min
- 2
Does a window even apply?
~43 minTake an unlabelled problem and say whether a window works, which kind, and why.
- LessonTest a problem for a window7 min
- LessonWho chooses the width?6 min
- LessonWhy one pass is enough5 min
- DrillDecide before you write13 min
- DrillFind the broken shrink12 min
- 3
Where the off-by-one lives
~42 minState a window’s width and count from its indices, and know what nothing does to it.
- LessonTwo edges and a width5 min
- LessonDo the six sums without thinking6 min
- DrillCount the edges9 min
- DrillWalk the window by hand12 min
- DrillNothing, one, or too few10 min
- 4
Write them, then refuse one
~50 minWrite both templates against real input, then meet a problem that needs neither.
- Build stepCount the busy windows14 min
- Build stepShrink to the shortest run16 min
- Build stepRefuse it before you solve it8 min
- Build stepCount the runs that come out level12 min
More in Algorithms & interviews
Keep going
Discrete Mathematics Drills
MA101 taught it once. This is the volume.
Intermediate~8h8 modules
Interview: Trees & Graphs
One skeleton. Then the family it answers.
Intermediate~5h6 modules
Interview: Dynamic Programming
Most problems are not new. Learn to see the shape.
Advanced~5h5 modules
System Design: Walkthroughs
The system is the setting. The decisions are the work.
Advanced~4h5 modules
Read the problem. Then say whether a window applies at all.
The first item is free. ~3h of focused work, at your own pace.
Intermediate~3h4 modules