You're in a timed interview. The problem appears on the screen. You have 30 seconds to choose your approach.
Question: "Given a sorted array, find two numbers that sum to a target value."
Your brain races:
- Hash map? Two pointers? Binary search?
- Is this a two pointers problem?
- How do I know for sure?
The fast solver recognizes the pattern in 5 seconds and starts coding.
The slow solver wastes 5 minutes trying different approaches.
The difference? Pattern recognition.
This guide will teach you a 5-second checklist to identify two pointers problems instantly, the keywords that signal the pattern, and the decision framework that works every time.
TL;DR
The 5-Second Checklist:
- ✅ Is the data sorted (or can I sort it)?
- ✅ Am I looking for pairs/triplets or subarrays?
- ✅ Can I make greedy decisions based on pointer positions?
- ✅ Do I need O(1) space?
- ✅ Are there keywords like "opposite ends," "converge," "fast/slow"?
If 3+ are YES → Two Pointers
The Pattern Recognition Framework
Step 1: Check the Data Structure
Two pointers works on:
- Arrays (sorted or unsorted)
- Strings
- Linked lists
Keywords to look for:
- "sorted array"
- "string"
- "linked list"
- "sequence"
Example:
- ✅ "Given a sorted array..." → Likely two pointers
- ✅ "Given a linked list..." → Possibly fast/slow pointers
- ✗ "Given a tree..." → Not two pointers
Step 2: Identify the Goal
Two pointers is used for:
- Finding pairs/triplets (Two Sum, 3Sum, 4Sum)
- Partitioning (Move Zeroes, Sort Colors)
- Palindrome checking (Valid Palindrome)
- Removing duplicates (Remove Duplicates)
- Cycle detection (Linked List Cycle)
- Finding middle (Middle of Linked List)
Keywords to look for:
- "find two numbers"
- "find all triplets"
- "move all X to the end"
- "is palindrome"
- "remove duplicates"
- "detect cycle"
- "find middle"
Step 3: Check for Sorting
Critical question: Is the array sorted?
- Sorted → Strong signal for two pointers
- Unsorted + can sort → Consider two pointers
- Unsorted + need indices → Hash map, not two pointers
Example:
- ✅ "Given a sorted array, find two numbers..." → Two pointers
- ✅ "Find all triplets that sum to zero" → Sort first, then two pointers
- ✗ "Return indices of two numbers..." → Hash map (can't sort)
Step 4: Space Constraints
Two pointers is ideal when:
- Problem requires O(1) space
- Problem says "in-place"
- Problem says "without extra space"
Example:
- ✅ "Do this in-place without extra space" → Two pointers
- ✅ "Space complexity must be O(1)" → Two pointers
- ⚠️ "No space constraint" → Hash map might be simpler
Step 5: Look for Variant Keywords
Opposite Direction:
- "from both ends"
- "converge"
- "start and end"
- "left and right"
Same Direction:
- "fast and slow"
- "tortoise and hare"
- "read and write"
- "sliding window"
Example:
- ✅ "Start from both ends..." → Opposite direction two pointers
- ✅ "Use fast and slow pointers..." → Same direction two pointers
The 5-Second Checklist
When you see a problem, run through this checklist:
✅ Checklist Item 1: Sorted Data?
Question: Is the array sorted, or can I sort it without losing required information?
- YES → +1 point for two pointers
- NO → Consider hash map
✅ Checklist Item 2: Pairs/Triplets/Subarrays?
Question: Am I looking for pairs, triplets, or contiguous subarrays?
- YES → +1 point for two pointers
- NO → Might be DP or other pattern
✅ Checklist Item 3: Greedy Decisions?
Question: Can I make a decision about which pointer to move based on current values?
- YES → +1 point for two pointers
- NO → Might need exhaustive search
✅ Checklist Item 4: O(1) Space?
Question: Does the problem require O(1) space or in-place modification?
- YES → +1 point for two pointers
- NO → Hash map might be simpler
✅ Checklist Item 5: Variant Keywords?
Question: Do I see keywords like "both ends," "fast/slow," "converge"?
- YES → +1 point for two pointers
- NO → Check other patterns
Scoring:
- 4-5 points: Definitely two pointers
- 3 points: Likely two pointers
- 1-2 points: Consider other patterns
- 0 points: Not two pointers
Problem Type Recognition
Type 1: Two Sum Family (Sorted Array)
Pattern:
- Sorted array
- Find pairs/triplets that sum to target
- Opposite direction pointers
Keywords:
- "sorted array"
- "two numbers"
- "sum to target"
Examples:
- Two Sum II (#167)
- 3Sum (#15)
- 4Sum (#18)
- 3Sum Closest (#16)
Template:
left, right = 0, len(nums) - 1
while left < right:
if condition:
return [left, right]
elif sum < target:
left += 1
else:
right -= 1Type 2: Palindrome Family
Pattern:
- String or array
- Check symmetry
- Opposite direction pointers
Keywords:
- "palindrome"
- "symmetric"
- "mirror"
Examples:
- Valid Palindrome (#125)
- Valid Palindrome II (#680)
Template:
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return TrueType 3: In-Place Manipulation
Pattern:
- Modify array in-place
- Read/write pointers
- Same direction
Keywords:
- "in-place"
- "without extra space"
- "remove"
- "move"
Examples:
- Remove Duplicates (#26)
- Move Zeroes (#283)
- Remove Element (#27)
Template:
write = 0
for read in range(len(nums)):
if condition:
nums[write] = nums[read]
write += 1
return writeType 4: Fast/Slow Pointers (Linked List)
Pattern:
- Linked list
- Cycle detection or finding middle
- Same direction, different speeds
Keywords:
- "linked list"
- "cycle"
- "middle"
- "fast and slow"
Examples:
- Linked List Cycle (#141)
- Middle of Linked List (#876)
- Happy Number (#202)
Template:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return FalseType 5: Container/Trapping Water
Pattern:
- Array of heights
- Maximize area or volume
- Greedy pointer movement
Keywords:
- "container"
- "water"
- "area"
- "volume"
Examples:
- Container With Most Water (#11)
- Trapping Rain Water (#42)
Template:
left, right = 0, len(height) - 1
max_area = 0
while left < right:
area = calculate_area(left, right)
max_area = max(max_area, area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_areaDecision Tree
Is the data sorted?
├─ YES → Is it about pairs/triplets?
│ ├─ YES → Two Pointers (opposite direction)
│ └─ NO → Check other patterns
└─ NO → Can I sort it?
├─ YES → Do I need original indices?
│ ├─ YES → Hash Map
│ └─ NO → Sort + Two Pointers
└─ NO → Is it a linked list?
├─ YES → Fast/Slow Pointers
└─ NO → Hash Map or other patternPractice: Identify the Pattern
For each problem, identify if it's two pointers in 5 seconds:
Problem 1
"Given a sorted array, find two numbers that sum to a target."
Analysis:
- ✅ Sorted
- ✅ Pairs
- ✅ Greedy (move based on sum)
- ✅ O(1) space possible
- ✅ Keywords: "sorted," "two numbers"
Answer: Two Pointers (opposite direction) ✓
Problem 2
"Given an unsorted array, return indices of two numbers that sum to target."
Analysis:
- ✗ Unsorted
- ✅ Pairs
- ✅ Need indices
- ✗ Can't sort (would lose indices)
Answer: Hash Map, not Two Pointers ✗
Problem 3
"Remove duplicates from a sorted array in-place."
Analysis:
- ✅ Sorted
- ✅ In-place
- ✅ O(1) space
- ✅ Keywords: "in-place," "remove"
Answer: Two Pointers (read/write) ✓
Problem 4
"Detect if a linked list has a cycle."
Analysis:
- ✅ Linked list
- ✅ Cycle detection
- ✅ Keywords: "cycle"
- ✅ O(1) space
Answer: Two Pointers (fast/slow) ✓
Problem 5
"Find the longest substring without repeating characters."
Analysis:
- ✅ Substring (contiguous)
- ✅ Can use two pointers
- ⚠️ But this is Sliding Window (a variant of two pointers)
Answer: Sliding Window (two pointers variant) ✓
Common Traps
Trap 1: Confusing Two Pointers with Sliding Window
Sliding Window is a specific type of two pointers where:
- Both pointers move in the same direction
- The "window" between them represents a contiguous subarray
- Used for substring/subarray problems
Two Pointers (general) includes:
- Opposite direction (converging)
- Same direction (fast/slow)
- Sliding window (special case)
Rule: If the problem asks for "substring" or "subarray," think Sliding Window first.
Trap 2: Assuming All Pair Problems Use Two Pointers
Counter-example: Two Sum (#1)
- Unsorted array
- Need original indices
- Use hash map, not two pointers
Rule: Check if the array is sorted and if you need indices.
Trap 3: Missing the Fast/Slow Variant
Keywords that signal fast/slow:
- "linked list"
- "cycle"
- "middle"
- "tortoise and hare"
Rule: Linked list + cycle/middle → Fast/slow pointers
Quick Reference Table
| Problem Type | Data Structure | Sorted? | Variant | Example |
|---|---|---|---|---|
| Two Sum | Array | Yes | Opposite | #167 |
| 3Sum | Array | Sort first | Opposite | #15 |
| Palindrome | String | N/A | Opposite | #125 |
| Remove Duplicates | Array | Yes | Read/Write | #26 |
| Move Zeroes | Array | No | Read/Write | #283 |
| Cycle Detection | Linked List | N/A | Fast/Slow | #141 |
| Find Middle | Linked List | N/A | Fast/Slow | #876 |
| Container Water | Array | No | Opposite | #11 |
Practice Strategy
To master pattern recognition:
- Solve 20 two pointers problems from different types
- Before coding, identify the variant (opposite, fast/slow, read/write)
- Create flashcards with problem statements and pattern type
- Time yourself: Can you identify the pattern in 5 seconds?
- Use LeetCopilot's pattern recognition to train on mixed problems
FAQ
Q: How do I know if it's two pointers or sliding window?
A: Sliding window is for substring/subarray problems where you're optimizing over contiguous segments. Two pointers (opposite direction) is for pairs/triplets or palindromes.
Q: What if I'm still not sure?
A: Check the constraints. If it says "sorted array" or "in-place," it's likely two pointers. If it says "substring" or "subarray," it's likely sliding window.
Q: Can I use two pointers on unsorted arrays?
A: Yes, for in-place manipulation (Move Zeroes, Remove Element) or if you can sort first (3Sum). But not for pair-finding if you need indices.
Q: How long does it take to master pattern recognition?
A: With focused practice, you can recognize most patterns in 1-2 weeks. Solve 3-5 problems per pattern type.
Conclusion
Pattern recognition is the skill that separates fast solvers from slow ones. Master the 5-second checklist, and you'll identify two pointers problems instantly.
The 5-Second Checklist:
- ✅ Sorted data?
- ✅ Pairs/triplets/subarrays?
- ✅ Greedy decisions?
- ✅ O(1) space?
- ✅ Variant keywords?
3+ YES → Two Pointers
Key patterns:
- Opposite direction: Sorted arrays, pairs, palindromes
- Fast/slow: Linked lists, cycles, middle
- Read/write: In-place manipulation, removing elements
Decision framework:
- Sorted + pairs → Two pointers
- Unsorted + indices → Hash map
- Linked list + cycle → Fast/slow
- In-place → Read/write
Master this framework, and you'll choose the right approach in seconds, not minutes. For more on two pointers, see the complete guide and two pointers vs sliding window.
Next time you see a problem, run the 5-second checklist. You'll know the answer before your interviewer finishes reading the question.
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
