Two pointers: how to spot it

Lesson 2.1 · Pattern lesson · 4 min · by Vinay C

We'll cover the following

Two indexes move through the data instead of one. Often they start at both ends of a sorted line and walk inward, so each step rules out many options at once.

How the question sounds

  • The input is sorted, or you can sort it for free
  • Find a pair or triplet that sums to a value (3Sum fixes one number, then runs two pointers)
  • Palindromes, or comparing the two ends of an array
  • Maximize something using the distance between two positions
  • Modify an array in place, keeping some items and skipping others

The two shapes

Where it shows up in GenAI work

Merging two ranked result lists, deduplicating sorted IDs from retrieval, and diffing two token sequences (for example two model outputs) all use two pointers.

Is it two pointers?

Decide yes or no before you open each answer.

Check yourselfGiven a sorted array, find two numbers that add up to a target.

Yes. Sorted plus a pair. One pointer at each end, move the one that fixes the sum.

Check yourselfCheck whether a string is a palindrome.

Yes. Compare the two ends and walk inward.

Check yourselfFind the longest substring with no repeating characters.

No. That is a contiguous range that grows and shrinks, which is a sliding window.

Check yourselfReturn the indices of two numbers in an unsorted array that add up to a target.

No. Unsorted, so pointers have nothing to steer by. A hash map of seen values fits.

Check yourselfRemove duplicates from a sorted array without extra memory.

Yes. Slow and fast pointers: fast reads, slow writes.

Check yourselfFind the number of ways to climb n stairs with steps of 1 or 2.

No. Each answer builds on earlier answers, which is dynamic programming.

Problems in this pattern

  1. Pair with a given sum · Easy · Opposite ends
  2. Valid palindrome · Easy · Opposite ends
  3. Container with most water · Medium · Opposite ends
  4. Remove duplicates in place · Easy · Slow and fast

Summary: say it in the interview

  • Opposite ends: L starts at the left, R at the right. They move toward each other based on a comparison.
  • Slow and fast: Both start at the left. fast scans every item and slow marks where the next kept item goes.