Learn · DSA · Pattern 2
Two Pointers
Instead of one finger scanning a list, you use two — and let them work toward each other. It turns a bunch of O(n²) problems into O(n), often with zero extra memory.
Before we start
- What the two-pointer pattern is and the two flavours it comes in
- How it solves “find a pair” on a sorted array with no extra memory
- The exact signals that scream “use two pointers”
- How it compares to the hashmap approach — and when each wins
- Solve palindrome, pair-sum, and “remove in place” problems
- Choose two pointers vs a hashmap on purpose (time vs space)
- Explain an O(n) time, O(1) space solution out loud
Why you’re learning it: it’s one of the highest-frequency interview patterns, and it teaches a key idea — when data is sorted, you can often skip the hashmap entirely and save memory. ⏱️ ~25 min. Do Arrays & Hashing and Big-O first.
What is the two-pointer pattern?
Imagine a sorted row of numbers. Put one finger on the far left (smallest) and one on the far right (largest). Now you have a dial: moving the left finger right makes your total bigger; moving the right finger left makes it smaller. Because the list is sorted, each move is a smart, informed step — never a blind scan.
The two flavours
Left starts at 0, right at the end; they move toward each other. Used for: pair-sum on sorted arrays, palindrome checks, “container with most water”.
Both start at the left; one moves faster. Used for: removing duplicates in place, detecting a loop in a linked list, finding the middle.
Watch it work — pair sum on a sorted array
Find two numbers in [1, 3, 5, 8, 12] that add to 9. L = left pointer, R = right pointer.
sum = 1 + 12 = 13. Too big (need 9) → move the RIGHT pointer left to shrink it.
sum = 1 + 8 = 9. That’s the target! Answer = indices [0, 3]. ✅
No nested loop, no hashmap — just two pointers converging. O(n) time, O(1) extra space.
When to reach for it — the triggers
Where you’ll use it — real life
Walking a bank file and a ledger together with two pointers is a classic reconciliation move — line them up without loading everything into a map.
Checking a string reads the same both ways is literally a left pointer and a right pointer marching inward.
Removing duplicates or blanks from a list without a second array — fast pointer reads, slow pointer writes.
“Container with most water”, “3-sum” — all built on the converging-pointer idea.
Two Pointers vs a Hashmap
| Hashmap (Pattern 1) | Two Pointers | |
|---|---|---|
| Needs sorting? | No | Yes (or already sorted) |
| Extra memory | O(n) — stores what it’s seen | O(1) — just two indices |
| Best when | Unsorted data, need counts/lookups | Sorted data, want to save memory |
Same problem, two tools. Knowing why you’d pick each is exactly the judgement interviews probe.
Why we practice this
The pattern is simple; recognising when it applies is the skill. A few reps and “sorted array + find a pair” instantly lights up as “two pointers”.
Now YOU do the reps
Out loud: “How do two converging pointers find a pair in a sorted array, and why is it O(1) space when a hashmap is O(n)?” Then log it in your Journal.
Back to your Siemens roadmap →