Two Pointers Pattern
The Two Pointers pattern uses two index variables that traverse a sorted array or string, usually moving toward each other from opposite ends or in the same direction at different speeds. It replaces nested loops on sorted input, dropping a brute-force O(n^2) solution down to O(n) time and O(1) extra space.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(n)
- Space
- O(1)
When to use Two Pointers
- The input is sorted (or you are allowed to sort it first) and you need a pair, triplet, or k-tuple that satisfies a condition.
- You are searching for a subarray or substring where the answer depends on both endpoints rather than a single index.
- You need to compare characters from both ends of a string, e.g. palindrome checks.
- You can reduce a brute-force O(n^2) nested loop on a one-dimensional input to a single pass.
- You need to partition an array in place (Dutch national flag, remove duplicates, move zeros).
Template
function twoPointers(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) {
return [left, right];
} else if (sum < target) {
left++;
} else {
right--;
}
}
return [-1, -1];
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 167 | Two Sum II - Input Array Is Sorted | medium | foundational |
| 15 | 3Sum | medium | frequently asked |
| 11 | Container With Most Water | medium | frequently asked |
| 125 | Valid Palindrome | easy | foundational |
| 26 | Remove Duplicates from Sorted Array | easy | foundational |
| 42 | Trapping Rain Water | hard | company favorite |
| 75 | Sort Colors | medium | classic |
| 283 | Move Zeroes | easy | foundational |
Common mistakes
- Applying Two Pointers to an unsorted array without first sorting (or recognizing that sorting would change the required output order).
- Forgetting to skip duplicates after moving a pointer in problems like 3Sum, which produces repeated tuples in the result.
- Using `<=` instead of `<` in the loop condition, which double-counts the same index in pair-sum problems.
- Moving the wrong pointer when the sum overshoots or undershoots the target.
- Mutating the array in place when the problem requires the original order to be preserved for later checks.
Frequently asked questions
When should I use Two Pointers vs Sliding Window?
Use Two Pointers when the input is sorted and the pointers move based on a comparison between the two endpoint values. Use Sliding Window when you need a contiguous subarray or substring whose size or content is governed by an aggregate condition (sum, count of distinct chars) and both pointers move forward only.
Does the array always have to be sorted?
No, but most Two Pointers problems either start with a sorted input or sort it first as a preprocessing step. Variants like Move Zeroes and Sort Colors use Two Pointers on unsorted data because they partition the array in place rather than search for a sum.
What is the time complexity of Two Pointers?
The traversal itself is O(n) because each pointer moves at most n times. If you sort the array first, the total cost becomes O(n log n) dominated by the sort. Space stays O(1) since both pointers are integer variables.
Can Two Pointers work on linked lists?
Yes, but the two pointers usually move at different speeds rather than from opposite ends, since you cannot index a linked list in O(1). That variant is called Fast and Slow Pointers and has its own canonical template.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Two Pointers pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →