The Two Pointers Pattern
Two indices walk the same array, usually from opposite ends toward each other or at different speeds. Each step, one pointer moves based on a comparison, so the pair sweeps the whole search space in a single pass instead of checking every combination.
8
Interactive problems2
Free to openHow to recognise a two pointers problem
The array is sorted, or sorting it does not lose information, and the question asks for a pair or triplet meeting some condition. "Find two numbers that sum to X", "the container holding the most water", "remove duplicates in place" are all this pattern. If your first instinct is a nested loop over the same array, check whether two pointers collapses it.
Time and space complexity
O(n) time and O(1) extra space, against O(n²) for the nested-loop version. When sorting is required first, the sort dominates at O(n log n).
Two Pointers practice problems
Each problem pairs a worked explanation with an interactive visualizer you step through yourself, plus the implementation in JavaScript, Python, Java and C++.
Related patterns and concepts
The underlying technique is covered from first principles in the two pointers lesson in the DSA visualizer curriculum. Two Pointers is one of the 16 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.
