SERIES
Two Pointers
Teach the two-pointer shape — how placing two indices at strategic positions and moving them toward each other (or apart) eliminates the inner loop and collapses O(n²) brute force into O(n).
In this seriesNEWSLETTER
01
Valid Palindrome: the two-pointer read from both ends
A string is a palindrome if it reads the same forwards and backwards after stripping non-alphanumeric characters. Two pointers placed at the ends and walked inward check this in O(n) time and O(1) space — no reversed copy needed.Jun 13, 2026 · 9 min read · #00012
02
Two Sum II: why sorted order makes two pointers work
The sorted input is not a coincidence — it's the contract the two-pointer approach depends on. Once you know the array is sorted, you can place pointers at both ends and move them with purpose: too large, shrink the right; too small, grow the left.Jun 13, 2026 · 10 min read · #00013
03
3Sum: two pointers inside a sorted loop
3Sum takes Two Sum II one level up: fix one element, then run Two Sum II on the rest. Sorting first lets you skip duplicates cleanly, which is the tricky part most implementations get wrong.Jun 13, 2026 · 12 min read · #00014
04
Container With Most Water: why you always move the shorter line
The greedy argument is what makes the two-pointer correct here — not just fast. Moving the taller line can only make things worse; moving the shorter one is the only way to possibly improve. That reasoning is the whole solution.Jun 13, 2026 · 12 min read · #00015
05
Trapping Rain Water: from O(n) space to O(1) with two pointers
The brute force is obvious. The prefix/suffix array approach is clean. The two-pointer solution is where it gets interesting — you can eliminate the auxiliary arrays entirely by recognising that you only need the running max from whichever side is shorter.Jun 13, 2026 · 15 min read · #00016
Occasional notes on what I'm building
Get an email when I publish a new post — engineering write-ups, no spam. Unsubscribe anytime.