Two pointers

Python Two Pointer Interview Questions

Two pointers means moving two positions through a sequence at once — from both ends, or at different speeds — instead of checking every possible pair. It is how a slow solution becomes a fast one when the input is sorted.

What two pointer questions ask you to do

Across the questions here, these are the shapes that come up:

  • Find a pair of values that adds up to a target
  • Remove duplicates from a sorted sequence in place
  • Merge two sorted sequences
  • Find a midpoint, or detect a loop
  • Split a sequence around a value

Common mistakes with two pointers

It only works on a sorted sequence. Given unsorted input it returns a wrong answer rather than an error, and sorting first changes the cost of the whole solution, so that has to be part of the answer rather than a detail left out of it.

Questions to practice

Roles this comes up in

Companies these come from

Other topics