Interview patterns
Two Pointers Pattern Explained for Coding Interviews
Updated Nov 27, 2026 · 3 min read
Put one pointer at each end of a sorted array. If the sum is too big, move the right pointer left; if it's too small, move the left pointer right; stop when it matches. Each step rules out one element for good, so the search takes O(n) time and O(1) extra space instead of checking every pair in O(n²).
Key points
- ●Spot it: sorted input, you need a pair or a triple, and moving one end pushes the sum one way.
- ●Sum too big: move right left. Too small: move left right.
- ●O(n) time, O(1) extra space, versus O(n²) for every pair.
- ●Unsorted input? Sort it first or use a hash map.

The riddle: 15 pairs or 3 looks
Maya has a sorted list: 1, 2, 4, 6, 8, 11. Which two numbers add up to 10?
She could check every pair. Six numbers make 15 pairs, so up to 15 tries. But the list is sorted, and that's the clue. With two pointers she finds the answer in three looks.
The slow way: check every pair
The brute force picks a first number, then tries every number after it. For n numbers that is about n² / 2 pairs. Fine for 6, painful for 100,000. It's O(n²), and it ignores the one thing the problem handed you: the order.
The idea: one finger on each end
Put the left pointer on the smallest number and the right pointer on the biggest. Add them.
- Sum equals the target: done.
- Sum too big: the right number is too large to pair with anything still in range, so move the right pointer one step left.
- Sum too small: the left number is too small to pair with anything still in range, so move the left pointer one step right.
Each move throws away one number for good, and you never have to look at it again. That's why it's safe: in a sorted list, moving the right end in can only make the sum smaller, and moving the left end in can only make it bigger. The flowchart on the visual sheet above is exactly these three branches.
Trace it on Maya's list
| Look | left | right | Sum | Move |
|---|---|---|---|---|
| 1 | 1 | 11 | 12 | too big, right steps left |
| 2 | 1 | 8 | 9 | too small, left steps right |
| 3 | 2 | 8 | 10 | match |
2 + 8 = 10 in three looks, not fifteen.
The Python template
def pair_sum(a, target):
l, r = 0, len(a) - 1
while l < r:
s = a[l] + a[r]
if s == target: return l, r
if s < target: l += 1
else: r -= 1
On [1, 2, 4, 6, 8, 11] with target 10 it returns (1, 4), the indexes of 2 and 8. If no pair exists, the pointers meet and the function returns None.
Complexity: O(n) time and O(1) extra space, against O(n²) for the brute force. The pointers only ever move inward, so together they take at most n steps, and you store two indexes and a sum.
How to spot it
- The input is a sorted array or a string.
- The task asks for a pair or a triple that meets a condition, like a target sum.
- Moving one end inward has a known direction of effect, so you can discard one side each step.
For a triple, as in 3Sum, fix one number and run the same two pointers on the numbers after it. For a palindrome check, the pointers compare letters instead of adding numbers, and both move in after every match.
It shows up in Two Sum II (LeetCode 167), Container With Most Water (11), 3Sum (15), Valid Palindrome (125) and Trapping Rain Water (42).
The trap
Using it on an unsorted array. Without the order, moving a pointer tells you nothing, and you can skip right past the answer. Sort the array first, or reach for a hash map instead. Sorting costs O(n log n), so if the problem wants the original indexes, the hash map is often the simpler pick.
Frequently asked questions
When should you use the two pointers technique?
When the input is a sorted array or a string, you need a pair or triple that meets a condition such as a target sum, and moving one end inward has a known effect on that condition.
Why does two pointers need a sorted array?
Sorting is what makes the move safe. In a sorted array, moving the right pointer left can only make the sum smaller, and moving the left pointer right can only make it bigger, so you can throw away one side each step.
What is the time complexity of two pointers?
O(n) time, because each step moves one pointer inward and they meet after at most n steps, and O(1) extra space. Checking every pair is O(n²).
Two pointers vs hash map: which should I use?
Two pointers if the input is already sorted, since it needs no extra memory. A hash map if it isn't sorted and you can't or don't want to sort it.