Two pointers interview questions
Two pointers replaces a nested loop with two indexes that move toward each other or in the same direction. It is the pattern behind many problems that look like they need quadratic time.
How do I solve two pointers interview questions?
Two pointers uses two indexes that move through a sequence, usually from both ends toward the middle or one behind the other, to avoid checking every pair. It works when order gives you a reason to move one pointer, typically on sorted data, palindromes and in-place rearrangement, in linear time.
How to recognise a two pointers problem
The input is sorted, or sorting it first is acceptable, and the question is about a pair or triple with a property.
The question is about a palindrome, reversing, merging two sorted sequences or removing items in place.
You are asked for constant extra space and the obvious approach copies the data.
How to build the solution
For a pair in sorted data, start one pointer at each end. If the current sum is too small move the left pointer right; if too large move the right pointer left. Each move discards candidates that cannot work.
For in-place filtering, use a slow pointer for where the next kept item goes and a fast pointer that scans. Copy when the fast pointer finds a keeper.
For triples, fix one element and run the pair technique on the rest, skipping duplicates, for quadratic total time.
State why it is correct: each pointer move rules out a set of pairs, so no valid answer is skipped.
Classic problems to practise
Valid Palindrome, Two Sum II (sorted input), 3Sum, Container With Most Water, Remove Duplicates from Sorted Array, Move Zeroes, Trapping Rain Water, Merge Sorted Array.
Mistakes interviewers see
Moving the wrong pointer. Reason about which pointer's movement can possibly improve the answer before you write the loop.
Off-by-one on the loop condition (left < right versus left <= right), which matters when the two meet on one element.
Not skipping duplicates in 3Sum, producing repeated triples, or skipping them in a way that drops valid ones.
Applying it to unsorted data without saying that it needs a sort, which changes the cost to n log n.
Practise it as an interview
Take a topic-based interview on two pointers to practise the pattern the way it is asked: say the pattern before you code, write and run the solution in a live editor, and answer the cross-questions about edge cases and complexity. The scored report shows whether you lost points explaining, coding or defending. Your first 3 single-question interviews are free for life, every interview report is free, and a new account also gets 6 welcome credits.
Frequently asked questions
How is two pointers different from a sliding window?
Two pointers moves indexes to find a pair or rearrange data, often from both ends. A sliding window keeps a contiguous range and grows or shrinks it while a condition holds. Both are linear.
Does two pointers need sorted input?
For the opposite-ends pair technique, yes. For the fast and slow form used in filtering and in linked lists, no.
Keep reading
- Arrays and hashing interview questions
- Sliding window interview questions
- Stack interview questions
- Binary search interview questions
- Linked list interview questions
- Trees interview questions
- Graphs interview questions
- Heap and priority queue interview questions
- Company interview guidesRounds and questions by company