Browse curriculum

Two Pointers

Two Pointers Interview Questions: 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. Below are the two pointers interview questions from LeetCode, each with a step-by-step visual solution.

37

Interactive problems

3

Free to open

Recognise

How do you 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.

Complexity

What is the time complexity of the two pointers pattern?

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).

Problems

Two Pointers interview questions: 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++.

Complexity

Two Pointers problems by difficulty and complexity

The difficulty, running time and extra space of every two pointers solution on this page, as each lesson derives them.

LessonDifficultyBestAverageWorstSpace
Introduction to Two PointersEasyΩ(1)Θ(n)O(n)O(1)
Reverse StringEasyΩ(n)Θ(n)O(n)O(1)
Reverse Vowels of a StringEasyΩ(n)Θ(n)O(n)O(n)
Reverse Words in a StringMediumΩ(n)Θ(n)O(n)O(n)
Two Sum II - Input Array Is SortedMediumΩ(1)Θ(n)O(n)O(1)
Max Number of K-Sum PairsMediumΩ(n log n)Θ(n log n)O(n log n)O(1)
Valid PalindromeEasyΩ(1)Θ(n)O(n)O(1)
Merge Strings AlternatelyEasyΩ(m + n)Θ(m + n)O(m + n)O(m + n)
Greatest Common Divisor of StringsEasyΩ(m + n)Θ(m + n)O(m + n)O(m + n)
Merge Sorted ArrayEasyΩ(n)Θ(m + n)O(m + n)O(1)
Remove Duplicates from Sorted ArrayEasyΩ(n)Θ(n)O(n)O(1)
String CompressionMediumΩ(n)Θ(n)O(n)O(1)
Remove ElementEasyΩ(n)Θ(n)O(n)O(1)
Rotate ArrayMediumΩ(n)Θ(n)O(n)O(1)
Next PermutationMediumΩ(n)Θ(n)O(n)O(1)
Repeated String MatchMediumΩ(n + m)Θ(n · (n + m))O(n · (n + m))O(n + m)
Find the Index of the First Occurrence in a StringEasyΩ(m)Θ(n · m)O(n · m)O(1)
Shortest PalindromeHardΩ(n)Θ(n)O(n)O(n)
Squares of a Sorted ArrayEasyΩ(n)Θ(n)O(n)O(n)
Is SubsequenceEasyΩ(1)Θ(n)O(n)O(1)
Valid Palindrome IIEasyΩ(n)Θ(n)O(n)O(1)
Longest Palindromic SubstringMediumΩ(n)O(n²)O(n²)O(1)
Palindromic SubstringsMediumΩ(n)Θ(n²)O(n²)O(1)
Encode and Decode StringsMediumΩ(n)Θ(n)O(n)O(n)
Container With Most WaterMediumΩ(n)Θ(n)O(n)O(1)
3-SumMediumΩ(n²)Θ(n²)O(n²)O(log n)
3Sum ClosestMediumΩ(n log n)Θ(n²)O(n²)O(1)
4SumMediumΩ(n³)Θ(n³)O(n³)O(1)
Valid Triangle NumberMediumΩ(n²)Θ(n²)O(n²)O(log n)
Move ZeroesEasyΩ(n)Θ(n)O(n)O(1)
Sort ColorsMediumΩ(n)Θ(n)O(n)O(1)
Trapping Rain WaterHardΩ(n)Θ(n)O(n)O(1)
Multiply StringsMediumΩ(m · n)Θ(m · n)O(m · n)O(m + n)
String to Integer (atoi)MediumΩ(1)Θ(n)O(n)O(1)
Add BinaryEasyΩ(max(m, n))Θ(max(m, n))O(max(m, n))O(max(m, n))
Add StringsEasyΩ(max(m, n))Θ(max(m, n))O(max(m, n))O(max(m, n))
Plus OneEasyΩ(1)Θ(n)O(n)O(1)
Curriculum

The underlying technique is covered from first principles in the two pointers lesson in the DSA visualizer curriculum. Two Pointers is one of the 18 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.

Directory

Every other coding interview pattern