Two Pointers
Two pointers places two indices on the same array or string — moving in opposite directions (closing in from both ends) or in the same direction (fast and slow) — to bring the time complexity down from O(n²) to O(n).
The technique shines on sorted data: at each step a single comparison decides which pointer to move, so no extra memory is needed. The hard part is choosing the right movement rule so it is both fast and never skips a valid answer.
Picture a sorted list of numbers with a finger at each end: if the two ends add up to more than the target, the right finger steps in to make the sum smaller; if less, the left finger moves forward to make it larger.
Because the list is sorted, each step takes a single comparison to know which side to move, so you never have to test every pair.
How to spot it
When you see these cues in the problem, reach for Two Pointers
| Keyword in the problem | Why it points to this topic |
|---|---|
| sorted array | Two pointers shine on sorted input |
| find pair / sum = target | Converge from both ends toward the middle |
| palindrome check | Compare characters from both ends inward |
| remove duplicates in place | Slow and fast pointers walk the same array |
When to use
4 typical situations where this topic applies
The array is sorted
You need to find a pair or triplet that satisfies a sum condition
You need to reverse or compare symmetrically (palindrome)
You need to remove duplicates in place
When NOT to use
3 situations where this topic doesn't fit — know them so you don't overuse them
The array is NOT sorted and does not need to be: a Hash Map is usually simpler to code
You need random-position queries: Two Pointers only scans linearly, so it does not fit
Counting subarrays by sum when the array has negative numbers: Prefix Sum + Hash Map fits better
Illustration
Edit the data, then press Play to see each step.
// Two pointers on a sorted array to find a pair that sums to targetlet left = 0, right = nums.length - 1while (left < right) { if (nums[left] + nums[right] === target) return [left, right] if (nums[left] + nums[right] < target) left++ else right--}return nullCode template
Most problems in this topic share this scaffold — copy it, then fill in the problem-specific logic.
function twoPointer(nums: T[]): R {
let left = 0
let right = nums.length - 1
while (left < right) {
if (condition(nums[left], nums[right])) {
return result(left, right)
}
if (shouldMoveLeft(nums[left], nums[right])) {
left++
} else {
right--
}
}
return notFound
}Recap
A quick summary — reread it before your interview
Key points
Initialize left=0, right=length-1 (opposite directions) or both at the start (same direction)
Compare the condition at the two pointers, then decide which one to move
Stop the loop when left >= right (opposite directions)
Common mistakes
Forgetting that the array must be sorted (if it is not, use a Hash Map)
Off-by-one in the left < right vs left <= right condition
Forgetting to skip over duplicates when you hit one
Complexity
Time O(n), Space O(1)Practice problems for this topic
Grouped by difficulty — work through them in order to build confidence