Coding interview pattern
Walk two indices toward or alongside each other to avoid a nested loop.
The idea
Two pointers replaces an O(n^2) pair-search with an O(n) walk by exploiting structure, usually sortedness. The two indices can move toward each other from the ends of a sorted array, or in the same direction at different speeds, or one per input in a merge. The key insight is that moving a pointer commits you to a decision: in a sorted two-sum, if the current pair sums too high you move the right pointer left, because every pair using that right element is now ruled out.
The converging variant (left starts at the front, right at the back) is the workhorse for sorted-array pair and triple problems, palindrome checks, and container-with-most-water style optimisation. The same-direction variant underlies in-place array partitioning, like moving zeros to the end or removing duplicates, where a slow pointer marks the write position and a fast pointer scans ahead.
What makes two pointers efficient is that each pointer traverses the array at most once, so even though there are two of them the combined movement is linear. The hard part in interviews is justifying why moving a particular pointer is safe; being able to articulate that invariant out loud is what earns the signal.
Recognition
Cost
| Operation | Time | Space |
|---|---|---|
| Converging scan (sorted input) | O(n) | O(1) |
| Two-pointer triple search (3Sum) | O(n^2) | O(1) extra |
| In-place partition / dedup | O(n) | O(1) |
Worked example
Translatable skeleton
Language-agnostic on purpose. Translate it to your language of choice.
left = 0; right = n - 1; while left < right: s = a[left] + a[right]; if s == target: return; elif s < target: left += 1; else: right -= 1Practice
Work these in order, easy to hard. Each is chosen to drill a different facet of the pattern.
| Problem | Level | Why it fits |
|---|---|---|
| Valid Palindrome | Easy | Converge from both ends, skipping non-alphanumerics. |
| Two Sum II (sorted) | Medium | The canonical converging two-sum. |
| Remove Duplicates from Sorted Array | Easy | Slow write pointer, fast read pointer. |
| 3Sum | Medium | Fix one element, two-pointer the rest. |
| Container With Most Water | Medium | Move the shorter wall inward each step. |
| Trapping Rain Water | Hard | Two pointers with running max on each side. |
Avoid these
In the interview
Recognising the pattern is half the score; saying the right things out loud is the other half. These are the sentences that earn the signal.
Lead by justifying why the array must be sorted for the pointer movement to be safe; if it is not sorted, say you will sort it first and account for the O(n log n) cost in your complexity.
When you move a pointer, state the elements you are permanently discarding and why they cannot be part of the answer; that running justification is the core signal of this pattern.
For the same-direction variant, name the two pointers explicitly as a read pointer and a write pointer so the in-place mutation logic stays clear to both you and the interviewer.
two pointer technique - opposite-direction pointers - fast and slow pointers array
An external resource we recommend. AlgoExpert is not affiliated with us and we earn nothing from this link.
Keep going
Track a contiguous subarray or substring without re-scanning it from scratch.
Halve the search space each step over any monotonic condition, not just sorted arrays.
Solve overlapping subproblems once, store the results, and build the answer up.
Explore level by level with a queue; the natural fit for shortest paths on unweighted graphs.
See all coding patterns, the coding questions for your role, and the behavioral round.