Skip to main content

Coding interview pattern

Two Pointers

Walk two indices toward or alongside each other to avoid a nested loop.

The idea

How the two pointers pattern works

Two pointers replaces an O(n^2) pair-search with an O(n) walk by exploiting structure, usually sortedness. The two indices can move toward each other from the ends of a sorted array, or in the same direction at different speeds, or one per input in a merge. The key insight is that moving a pointer commits you to a decision: in a sorted two-sum, if the current pair sums too high you move the right pointer left, because every pair using that right element is now ruled out.

The converging variant (left starts at the front, right at the back) is the workhorse for sorted-array pair and triple problems, palindrome checks, and container-with-most-water style optimisation. The same-direction variant underlies in-place array partitioning, like moving zeros to the end or removing duplicates, where a slow pointer marks the write position and a fast pointer scans ahead.

What makes two pointers efficient is that each pointer traverses the array at most once, so even though there are two of them the combined movement is linear. The hard part in interviews is justifying why moving a particular pointer is safe; being able to articulate that invariant out loud is what earns the signal.

Recognition

When to reach for it

  • The array is sorted (or you can sort it) and you need pairs or triples meeting a condition.
  • You need to do an in-place transformation with a read pointer and a write pointer.
  • You are checking symmetry, like a palindrome, from both ends inward.
  • A brute-force solution compares all pairs and you want to cut a factor of n.

Cost

Time & space complexity

OperationTimeSpace
Converging scan (sorted input)O(n)O(1)
Two-pointer triple search (3Sum)O(n^2)O(1) extra
In-place partition / dedupO(n)O(1)

Worked example

Walkthrough: Two Sum on a sorted array

  1. 1Place left at index 0 and right at the last index.
  2. 2Compute the sum of the two pointed elements.
  3. 3If it equals the target, return the pair.
  4. 4If it is too small, move left rightward to increase the sum; if too large, move right leftward to decrease it.
  5. 5Each move permanently discards one element from consideration, so the scan finishes in a single linear pass.

Translatable skeleton

The shape in pseudocode

Language-agnostic on purpose. Translate it to your language of choice.

Code
left = 0; right = n - 1; while left < right: s = a[left] + a[right]; if s == target: return; elif s < target: left += 1; else: right -= 1

Practice

Curated problem set

Work these in order, easy to hard. Each is chosen to drill a different facet of the pattern.

ProblemLevelWhy it fits
Valid PalindromeEasyConverge from both ends, skipping non-alphanumerics.
Two Sum II (sorted)MediumThe canonical converging two-sum.
Remove Duplicates from Sorted ArrayEasySlow write pointer, fast read pointer.
3SumMediumFix one element, two-pointer the rest.
Container With Most WaterMediumMove the shorter wall inward each step.
Trapping Rain WaterHardTwo pointers with running max on each side.

Avoid these

Common pitfalls

  • Forgetting to sort first when the pattern depends on order.
  • Off-by-one or infinite loops when the pointers cross.
  • Not skipping duplicates in problems like 3Sum, producing repeated triples.
  • Moving the wrong pointer and breaking the optimisation invariant.

In the interview

How to communicate it live

Recognising the pattern is half the score; saying the right things out loud is the other half. These are the sentences that earn the signal.

1

Lead by justifying why the array must be sorted for the pointer movement to be safe; if it is not sorted, say you will sort it first and account for the O(n log n) cost in your complexity.

2

When you move a pointer, state the elements you are permanently discarding and why they cannot be part of the answer; that running justification is the core signal of this pattern.

3

For the same-direction variant, name the two pointers explicitly as a read pointer and a write pointer so the in-place mutation logic stays clear to both you and the interviewer.

Also searched as

two pointer technique - opposite-direction pointers - fast and slow pointers array

Last reviewed by the site editor: June 2026

Practise these patterns on AlgoExpert
Recommended
200+ video-explained coding interview questions organised by the patterns covered on this page, with timed practice and solution walkthroughs.
Start practising

An external resource we recommend. AlgoExpert is not affiliated with us and we earn nothing from this link.

Sources

  1. Two Pointer Technique Deeparnab Chakrabarty, Dartmouth College
  2. 2.3 Quicksort (in-place partitioning) Sedgewick and Wayne, Princeton University
  3. 3SUM Wikipedia

Round out your prep

See all coding patterns, the coding questions for your role, and the behavioral round.