Coding interview pattern
Two Pointers
Walk two indices toward or alongside each other to avoid a nested loop.
The idea
How the two pointers pattern works
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
When to reach for it
- The array is sorted (or you can sort it) and you need pairs or triples meeting a condition.
- You need to do an in-place transformation with a read pointer and a write pointer.
- You are checking symmetry, like a palindrome, from both ends inward.
- A brute-force solution compares all pairs and you want to cut a factor of n.
Cost
Time & space complexity
| 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
Walkthrough: Two Sum on a sorted array
- 1Place left at index 0 and right at the last index.
- 2Compute the sum of the two pointed elements.
- 3If it equals the target, return the pair.
- 4If it is too small, move left rightward to increase the sum; if too large, move right leftward to decrease it.
- 5Each move permanently discards one element from consideration, so the scan finishes in a single linear pass.
Translatable skeleton
The shape in pseudocode
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
Curated problem set
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
Common pitfalls
- Forgetting to sort first when the pattern depends on order.
- Off-by-one or infinite loops when the pointers cross.
- Not skipping duplicates in problems like 3Sum, producing repeated triples.
- Moving the wrong pointer and breaking the optimisation invariant.
In the interview
How to communicate it live
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.
Also searched as
two pointer technique - opposite-direction pointers - fast and slow pointers array
Last reviewed by the site editor: June 2026
An external resource we recommend. AlgoExpert is not affiliated with us and we earn nothing from this link.
Sources
- Two Pointer Technique Deeparnab Chakrabarty, Dartmouth College
- 2.3 Quicksort (in-place partitioning) Sedgewick and Wayne, Princeton University
- 3SUM Wikipedia
Keep going
Related patterns
These techniques share moves with this one, so the practice carries over.
- Sliding Window
Track a contiguous subarray or substring without re-scanning it from scratch.
- Binary Search
Halve the search space each step over any monotonic condition, not just sorted arrays.
- Dynamic Programming
Solve overlapping subproblems once, store the results, and build the answer up.
- Breadth-First Search (BFS)
Explore level by level with a queue; the natural fit for shortest paths on unweighted graphs.
Round out your prep
See all coding patterns, the coding questions for your role, and the behavioral round.