What is Two Pointers?
The Two Pointers pattern uses two pointers to traverse an array or string, typically moving towards each other or in the same direction. It transforms O(n²) brute force solutions into O(n) optimal ones.Quick Recognition: If you see a sorted array + need to find pairs/triplets + want O(1) space, Two Pointers is likely the answer!
Pattern Recognition Checklist
Before using Two Pointers, ask yourself:Use Two Pointers When
- Array is sorted (or can be sorted)
- Need to find pairs with a property
- In-place modification required
- Need to compare elements from ends
- Want to avoid O(n²) nested loops
Don't Use When
- Need to preserve original order
- Array is unsorted and sorting changes answer
- Need to find subarrays (use Sliding Window)
- Need lookup by value (use HashMap)
When to Use
Sorted Arrays
Finding pairs, triplets, or elements with specific sum
Palindrome Problems
Checking or creating palindromes
Merging Arrays
Merging sorted arrays or linked lists
Removing Duplicates
In-place array modifications
Pattern Variations
1. Opposite Direction (Converging)
Pointers start at both ends and move towards the center.2. Same Direction (Fast & Slow)
Both pointers move in the same direction at different speeds.3. Container With Most Water
4. Valid Palindrome
5. 3Sum
6. Trapping Rain Water
7. Move Zeroes
8. Sort Colors (Dutch National Flag)
Classic Problems
1. Two Sum II (Sorted Array)
1. Two Sum II (Sorted Array)
Problem: Find two numbers in a sorted array that add up to target.Approach: Use converging pointers. If sum is too small, move left pointer right. If too large, move right pointer left.Time: O(n) | Space: O(1)
2. Container With Most Water
2. Container With Most Water
Problem: Find two lines that form a container holding the most water.Approach: Start from both ends. Move the pointer with the shorter line inward (greedy choice).Time: O(n) | Space: O(1)
3. Valid Palindrome
3. Valid Palindrome
Problem: Check if a string is a palindrome (ignoring non-alphanumeric).Approach: Compare characters from both ends, skip non-alphanumeric.Time: O(n) | Space: O(1)
4. 3Sum
4. 3Sum
Problem: Find all unique triplets that sum to zero.Approach: Sort array, fix one element, use two pointers for remaining two.Time: O(n^2) | Space: O(1) excluding output
5. Trapping Rain Water
5. Trapping Rain Water
Problem: Calculate trapped water between elevation bars.Approach: Two pointers with max height tracking from both sides.Time: O(n) | Space: O(1)
Template Code
Common Mistakes
Debugging Checklist
When your Two Pointers solution fails, check:1
Initialization
Are
left and right starting at correct positions?2
Loop Condition
Should it be
left < right or left <= right?3
Pointer Movement
Is at least one pointer moving in each iteration?
4
Edge Cases
Did you handle empty array, single element, all same elements?
5
Duplicates
Are you skipping duplicates correctly (for unique results)?
Complexity Quick Reference
| Problem Type | Time | Space | Key Insight |
|---|---|---|---|
| Two Sum (sorted) | O(n) | O(1) | Move based on sum comparison |
| 3Sum | O(n²) | O(1) | Fix one, two-pointer on rest |
| Remove Duplicates | O(n) | O(1) | Slow = write position |
| Container Water | O(n) | O(1) | Move shorter side inward |
| Trap Rain Water | O(n) | O(1) | Track max from both sides |
| Valid Palindrome | O(n) | O(1) | Skip non-alphanumeric |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Two Sum II | Amazon, Google | Basic converging |
| Valid Palindrome | Meta, Microsoft | Character comparison |
| Move Zeroes | Meta, Amazon | Fast-slow technique |
| Remove Duplicates | All FAANG | In-place modification |
| Squares of Sorted Array | Amazon | Converging with squares |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I notice the array is sorted, which suggests Two Pointers might work here.”
- “I’ll use two pointers starting at opposite ends.”
- “If the sum is too small, I move left pointer right to increase it.”
- “If too large, I move right pointer left to decrease it.”
- “This gives us O(n) time and O(1) space.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Can you do better than O(n²)?” | Two Pointers or HashMap |
| ”Can you do it in O(1) space?” | Two Pointers (not HashMap) |
| “The array is sorted” | Definitely Two Pointers! |
| ”Find a pair/triplet” | Two Pointers likely |
| ”Modify array in-place” | Fast-slow pointers |
Common Follow-ups
Common Follow-ups
Be ready for these follow-ups:
- “What if there are duplicates?” → Skip duplicates in loop
- “What if no solution exists?” → Return appropriate default
- “Can you handle negative numbers?” → Usually yes, same logic
- “What if array is not sorted?” → Sort first, or use HashMap
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Two Sum II | Easy | LeetCode 167 |
| Valid Palindrome | Easy | LeetCode 125 |
| 3Sum | Medium | LeetCode 15 |
| Container With Most Water | Medium | LeetCode 11 |
| Trapping Rain Water | Hard | LeetCode 42 |
Practice Roadmap
1
Day 1: Basics
- Solve: Two Sum II, Valid Palindrome
- Focus: Understanding when to move which pointer
2
Day 2: Fast-Slow
- Solve: Move Zeroes, Remove Duplicates
- Focus: In-place modifications
3
Day 3: Multi-pointer
- Solve: 3Sum, Sort Colors
- Focus: Managing multiple pointers
4
Day 4: Advanced
- Solve: Container With Water, Trapping Rain Water
- Focus: Greedy decisions with pointers