Two Pointers: One Pattern That Solves Dozens of Problems
Master the two pointers technique and unlock a single mental model that cracks pair sums, palindromes, dedup, and merging in linear time.
If you have ever stared at a problem and reached for a nested loop on instinct, two pointers is the upgrade you have been missing. It is one of the highest-leverage patterns in interview prep: a single idea that collapses a whole family of array and string problems from quadratic time down to linear.
The core move is simple. Instead of comparing every element against every other element, you keep two indices and move them intelligently based on what you observe. No extra data structures, no hashing, just disciplined pointer movement.
The three flavors you actually see
Most two pointer problems are one of these shapes:
- Opposite ends. Start one pointer at the left, one at the right, and walk them toward each other. Classic for sorted pair sums, container with most water, and palindrome checks.
- Same direction (fast and slow). Both start near the front; one runs ahead. Great for cycle detection, removing duplicates in place, and finding the nth node from the end of a linked list.
- Two sequences. One pointer per array, advancing whichever is behind. This is the engine behind merging sorted lists and intersection problems.
Why it works: monotonicity
The opposite-ends variant only works when the input has structure you can exploit, usually sortedness. Take the sorted two-sum: if the current pair sums too high, the only way to shrink it is to move the right pointer left. If it sums too low, move the left pointer right. Each element is visited at most once, so you get O of n time with O of 1 extra space.
That is the whole trick. You are pruning half the search space with every decision because the data guarantees you can never miss a valid answer.
A concrete template
For opposite-ends problems, the skeleton barely changes:
- Set
left = 0andright = n - 1. - While
left < right, inspect the pair. - Move whichever pointer makes progress toward the goal.
- Stop when the pointers cross.
For same-direction dedup, you keep a write pointer for the next slot to fill and a read pointer scanning ahead. You only advance write when read finds something new. The array is rebuilt in place with zero extra allocation.
When NOT to reach for it
Two pointers needs either sorted input or a relationship you can move monotonically. If the answer depends on arbitrary subsets or unsorted lookups, a hash map is usually the better tool. And if you find yourself adding a third pointer that jumps around unpredictably, you have probably left the pattern and should rethink.
If you want to feel the payoff, merging is a perfect first rep. Our merge intervals challenge leans on the same sort-then-sweep instinct that makes two pointers click, and it shows up constantly in real interviews.
Build the muscle
Patterns stick through volume, not theory. Skim the Algorithms track to see the variants laid out side by side, then follow a structured roadmap so you are not solving random problems in a vacuum. Once it feels automatic, check where you land on the leaderboard against solvers at your tier.
Two pointers is the cheapest big win in your prep. Spend an afternoon on it and you will spot it everywhere. Ready to test it under pressure? Drop into the arena and see if you can still beat the machine.