Two Pointers Interview Pattern Chooser
Two pointers interview questions are a family, not one trick. Opposite-end scans on sorted arrays, slow/fast on linked lists, and left/right grow patterns that overlap sliding window all get labeled “two pointers.” This chooser helps you pick the right subtype and explain it in the first minutes of a coding interview.
Core idea in one line
Maintain two indices (or node references) whose movement monotonically progresses so each element is processed a constant number of times—usually O(n) after any required sort.
Talk track:
“I’ll use two pointers so each index only moves forward; that keeps the scan linear.”
Monotonic movement is the contract. If both pointers bounce randomly, you no longer have the O(n) argument.
Subtype menu
| Subtype | Typical cue | Movement |
|---|---|---|
| Opposite ends | sorted array, pair sum, container water | left++ or right-- by comparison |
| Same direction | remove duplicates in-place, partition | read ahead, write behind |
| Slow / fast | cycle detection, middle of list | fast = fast.next.next |
| Window edges | contiguous constraint | left/right (see sliding window) |
| Merge sorted | merge two arrays/lists | advance smaller head |
If you need random access jumps based on values without a monotonicity argument, two pointers may not apply—consider a hash map or sort-plus-scan with a clear tradeoff statement.
Opposite ends: decision rule
Works when:
- Array is sorted (or you may sort and can restore indices if needed)
- Moving the pointer that is “too small” or “too big” is safe
- You can argue you never need to reconsider a discarded side
Pair-sum talk track:
“If sum is too small, left++ because the array is sorted; if too big, right--.”
Container-with-most-water style: move the shorter height pointer—and say why: width always decreases, so only a taller line can improve area.
Same-direction (read/write)
Use when transforming an array in place:
write ← 0
for read in 0..n-1:
if a[read] should be kept:
a[write] ← a[read]
write += 1
return write # new length
Narrate the invariant: “everything before write is the kept prefix.” Interviewers listen for that invariant more than for clever index math.
Slow / fast on lists
- Middle node: fast hits end, slow is mid
- Cycle: fast meets slow
- nth from end: lead pointer by n, then walk together
Always clarify null checks and even/odd length behavior out loud. Drawing two or three nodes beats hand-waving.
Two pointers vs sliding window vs binary search
| Need | Prefer |
|---|---|
| Contiguous segment with running state | Sliding window |
| Sorted pair / partition / in-place filter | Two pointers |
| Find boundary in monotonic predicate | Binary search |
| Unsorted pair sum without sorting allowed | Hash map |
Sorting to enable two pointers changes complexity to O(n log n) and can scramble original indices—mention tradeoffs before you sort.
First five minutes
- Sorted? Contiguous? Linked list?
- Name subtype
- State what makes each pointer move
- Confirm O(n) after sort if any
- Walk one example with finger indices on the pad
- List empty / single-element / all-duplicates cases
Worked micro-example (sorted two-sum)
Array: 2, 7, 11, 15, target 9
- left at 2, right at 15 → sum 17 too big → right--
- left at 2, right at 11 → sum 13 too big → right--
- left at 2, right at 7 → sum 9 → done
Narrate each comparison. Silence during pointer moves is where bugs hide.
Common boundary bugs
- Infinite loop: neither pointer moves on equality edge cases
- Off-by-one when returning indices vs values
- Sorting when the prompt forbids changing relative order
- Fast pointer null deref: check
fastandfast.next - Using opposite ends on unsorted data without sorting
- Forgetting duplicates policy (unique pairs vs all pairs)
Freeze recovery
- Draw the array and two fingers
- Write the move rule in a comment before code
- If the move rule is unclear, reconsider hash map / window
- Dry-run the equality case (
sum == target) explicitly
Talk track:
“On equality I’ll record the pair, then move left and skip duplicates.”
Practice plan
| Session | Focus |
|---|---|
| 1 | Two-sum II style sorted pair |
| 2 | In-place dedupe / partition |
| 3 | Linked-list mid + nth-from-end |
| 4 | Timed classify: window vs two pointers vs hash |
Soft practice note
The chooser skill is verbal: subtype, move rule, invariant. If you want a Chrome side panel to practice that narration under live pacing pressure, Luna Interview is built for calm rehearsal. Use it to strengthen communication—not to skip pattern practice or to violate interview rules.
Day-of checklist
- [ ] Subtype named
- [ ] Sorted/list/contiguous clarified
- [ ] Move rule for each pointer
- [ ] Null / empty / one-element cases
- [ ] Duplicates policy
- [ ] Complexity including sort if used
Pick the subtype first; the loop is just disciplined pointer motion after that.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.