Two Pointers Problems for Coding Interviews

Two pointers turns a quadratic scan into a linear one by moving two indexes through the data with a rule for which one advances. It works on sorted arrays, on palindromes and when partitioning in place.

Decide what each pointer means and why the move is safe (it never skips a better answer). Practise the variants: opposite ends, same direction (fast and slow) and one pointer reading while the other writes.

Coding problems

Related

Frequently asked questions

How do I get good at two pointers problems?

Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.

Can I run two pointers problems online here?

Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.