Pattern

Two pointers

Walk two positions through a sequence, moving one or both based on what you compare, to avoid a nested-loop scan.

What it is

Two pointers is a family of single- or dual-pass techniques where you track two positions in an array, string, or linked list and advance them according to a rule, instead of comparing every pair with a nested loop. The two pointers might start at opposite ends and converge (classic for sorted arrays), move at different speeds (fast/slow, useful for linked lists), or both move forward while one lags to overwrite in place (in-place filtering and merging). What makes it work is that a comparison at the pointers tells you, with certainty, which pointer to move — so you never have to revisit work already done.

When it applies

Tells in the problem

The classic mistake

The error interviewers commonly see is moving the "wrong" pointer, or moving both pointers on every step regardless of the comparison, which either misses the true answer or degrades the approach back into a nested-loop scan in disguise. It happens because the mechanical part — "advance a pointer" — is easy to write, but the proof that moving one specific pointer (not the other, not both) can never discard the true answer is the part actually being tested, and skipping that proof leads to moving pointers arbitrarily. Before coding, state the invariant out loud: "if I move this pointer inward, what class of answers am I certain I'm not losing, and why?" If you can't answer that in one sentence, you don't yet know which pointer to move.

Questions to start with

All 11 two pointers questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.