Two Pointers Interview Pattern Chooser

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

SubtypeTypical cueMovement
Opposite endssorted array, pair sum, container waterleft++ or right-- by comparison
Same directionremove duplicates in-place, partitionread ahead, write behind
Slow / fastcycle detection, middle of listfast = fast.next.next
Window edgescontiguous constraintleft/right (see sliding window)
Merge sortedmerge two arrays/listsadvance 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:

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

Always clarify null checks and even/odd length behavior out loud. Drawing two or three nodes beats hand-waving.

NeedPrefer
Contiguous segment with running stateSliding window
Sorted pair / partition / in-place filterTwo pointers
Find boundary in monotonic predicateBinary search
Unsorted pair sum without sorting allowedHash 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

  1. Sorted? Contiguous? Linked list?
  2. Name subtype
  3. State what makes each pointer move
  4. Confirm O(n) after sort if any
  5. Walk one example with finger indices on the pad
  6. List empty / single-element / all-duplicates cases

Worked micro-example (sorted two-sum)

Array: 2, 7, 11, 15, target 9

Narrate each comparison. Silence during pointer moves is where bugs hide.

Common boundary bugs

Freeze recovery

  1. Draw the array and two fingers
  2. Write the move rule in a comment before code
  3. If the move rule is unclear, reconsider hash map / window
  4. 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

SessionFocus
1Two-sum II style sorted pair
2In-place dedupe / partition
3Linked-list mid + nth-from-end
4Timed 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

Pick the subtype first; the loop is just disciplined pointer motion after that.