Two pointers means walking through an array or string with two indices at once, instead of one index inside a nested loop. Each step moves one of them, and each move safely rules out part of the input. The result: an O(n²) "try every pair" solution becomes a single O(n) pass.
What is a "pointer" here?
Just an index: a number that says where you are in the array. left = 0 means "I'm looking at the first item". Moving a pointer means changing that number (left++).
The problem it solves
Many problems ask about pairs: two characters that should match, two numbers that should add up to something, two walls that hold water. The naive way checks every pair:
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// check the pair (i, j)
}
}That's about n × n / 2 pairs: O(n²). For 100,000 items, that's 5 billion checks.
Two pointers check pairs cleverly: after looking at one pair, they know which side can be thrown away, so they never need to go back.
The main shape: start at both ends, move inward
let left = 0
let right = items.length - 1
while (left < right) {
// look at items[left] and items[right]
if (/* left side can't be part of the answer */) left++
else right--
}Each step moves one pointer one place inward, so after at most n steps they meet. That's O(n) time and O(1) extra memory.
When can you throw one side away?
This is the whole trick, and the part interviewers care about. You need a reason why the item you're dropping can't be part of a better answer. The three problems here each have their own reason:
| Problem | What you compare | Why one side can be dropped |
|---|---|---|
| Valid Palindrome | first char vs last char | Once a pair matches, it's done. Move both inward. |
| 3Sum (sorted array) | sum of the smallest and largest left | Sum too small? Only a bigger number helps, so move left right. Too big? Move right left. |
| Container With Most Water | two walls | The shorter wall already did its best with the farthest partner, so drop it. |
Sorting unlocks two pointers
In 3Sum, the pointer moves only make sense because the array is sorted. In a sorted array:
- moving
leftto the right can only make the value bigger, - moving
rightto the left can only make the value smaller.
So "sum too small → move left, sum too big → move right" always heads toward the target. If a problem asks for pairs with a certain sum and the order of the input doesn't matter, sort first and two pointers often follow. Sorting costs O(n log n), which is still much better than O(n²).
How to recognize this category
Think "two pointers" when the problem:
- compares the start and end of something (palindromes, reversing)
- asks for a pair or triplet with a given sum, especially in a sorted array
- wants the best value over pairs of positions where width or distance matters
- asks for O(1) extra memory on an array or string
Common mistakes
- Forgetting
left < rightin inner loops, so a pointer runs off the end or the two cross. - Moving the wrong pointer. Always be able to say why the side you drop can't be in a better answer. If you can't explain it, the move is probably wrong.
- Using two pointers on an unsorted array when the logic depends on order. Sort first.
Next up
Sliding Window is a close cousin: both pointers move in the same direction, marking the start and end of a range.