What is the Two Pointers Pattern?
The Two Pointers pattern is one of the most common techniques in coding interviews. It involves using two indices (pointers) to traverse a data structure, typically an array or string, in a coordinated way. The pattern is particularly useful for problems involving:
- Sorted arrays
- Finding pairs or triplets that satisfy a condition
- Comparing elements from both ends
- Sliding window variants
When to Use Two Pointers
| Problem Type | Indicator | Example |
|---|---|---|
| Pair sum in sorted array | "Find two numbers that sum to target" | Two Sum II |
| 3Sum / k-Sum | "Find triplets that sum to zero" | 3Sum, 4Sum |
| Container area | "Maximize area between lines" | Container With Most Water |
| Palindrome check | "Is this string a palindrome?" | Valid Palindrome |
| Merge sorted arrays | "Merge two sorted arrays" | Merge Sorted Array |
| Remove duplicates | "Remove duplicates in-place" | Remove Duplicates |
Two Pointers Variations
1. Opposite Direction (Left + Right)
Start one pointer at the beginning and one at the end. Move them toward each other based on a condition.
function twoSumSorted(nums, target) {
let left = 0, right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) return [left, right];
if (sum < target) left++;
else right--;
}
return null;
}
2. Same Direction (Slow + Fast)
Both pointers start at the beginning but move at different speeds. Useful for removing duplicates or finding cycles.
function removeDuplicates(nums) {
let slow = 0;
for (let fast = 1; fast < nums.length; fast++) {
if (nums[fast] !== nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
}
return slow + 1; // new length
}
3. Partition (Three-Way)
Three pointers partition an array into sections. Used in Dutch National Flag, 3-way quicksort.
Top Two Pointers LeetCode Problems
| Problem | Difficulty | Pattern |
|---|---|---|
| 167. Two Sum II | Medium | Opposite |
| 15. 3Sum | Medium | Opposite + Fix one |
| 11. Container With Most Water | Medium | Opposite |
| 125. Valid Palindrome | Easy | Opposite |
| 88. Merge Sorted Array | Easy | Opposite (from end) |
| 26. Remove Duplicates | Easy | Same direction |
| 42. Trapping Rain Water | Hard | Opposite |
| 75. Sort Colors | Medium | Partition |
How GhOst Helps with Two Pointers Problems
During live interviews, GhOst recognizes Two Pointers problems instantly and provides:
- Pattern identification: "This is a Two Pointers problem — use left/right from both ends"
- Optimal solution: O(n) time, O(1) space algorithm
- Edge cases: Empty array, single element, all duplicates
- Complexity analysis: Instant Big O breakdown
- Code template: Boilerplate that you can adapt quickly
Common Mistakes
- Using Two Pointers on an unsorted array when sorting first would help
- Forgetting to handle the case where left === right
- Not checking for integer overflow when summing large numbers
- Modifying the array while iterating when you shouldn't
Frequently Asked Questions
Yes, when used correctly. Each element is visited at most once by each pointer, giving O(n) total time with O(1) extra space.
For pair sum problems, you typically need to sort first (O(n log n)), then use Two Pointers (O(n)). If you cannot sort, use a Hash Map approach instead.
Two Pointers moves pointers independently based on conditions. Sliding Window maintains a contiguous subarray/substring with both pointers moving in the same direction.
GhOst analyzes the problem statement keywords ("sum to target", "container", "palindrome", "merge sorted") and instantly suggests the Two Pointers approach with code template.