Interview frequency: ⭐⭐⭐⭐⭐ (Top 5 pattern)
Many candidates fail interviews not because they don't know algorithms, but because they don't recognise when to use Two Pointers.
What is the Two Pointers technique?
Instead of using one index:
i = 0
you use two indices.
left = 0
right = len(arr) - 1
These pointers move based on the problem.
Example:
Index : 0 1 2 3 4
Array : 2 4 7 9 15
↑ ↑
left right
The pointers may:
- Move towards each other
- Move in the same direction
- Move at different speeds
When should you think of Two Pointers?
This is the most important part.
Whenever you see:
- Sorted array
- Pair problems
- Remove duplicates
- Move elements
- Reverse array/string
- Palindrome
- Merge sorted arrays
Your brain should immediately ask:
"Can I solve this using two pointers?"
Pattern 1: Opposite Direction
Pointers start from both ends.
L ------------->
<------------- R
Used for:
- Pair Sum
- Reverse Array
- Palindrome
- Container With Most Water
Example 1: Reverse an Array
Input
nums = [1,2,3,4,5]
Instead of creating another array, swap from both ends.
left = 0
right = len(nums)-1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
Output
[5,4,3,2,1]
Complexity
Time : O(n)
Space: O(1)
Interviewers love in-place solutions.
Example 2: Valid Palindrome
madam
m == m
a == a
d
Algorithm
left = 0
right = len(s)-1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
Time
O(n)
Very common interview problem.
Example 3: Two Sum II (Sorted Array)
Input
[2,4,7,11,15]target=18
Instead of checking every pair:
2 + 15 = 17
Need larger
Move left
4 + 15 = 19
Too large
Move right
4 + 11 = 15
Need larger
Move left
7 + 11 = 18
Found
Code
left = 0
right = len(nums)-1
while left < right:
total = nums[left] + nums[right]
if total == target:
return [left,right]
elif total < target:
left += 1
else:
right -= 1
Notice how the sorted property lets us decide which pointer to move.
Pattern 2: Same Direction
Used for:
- Remove duplicates
- Move zeroes
- Partition arrays
Example:
[0,1,0,3,12]
Expected
[1,3,12,0,0]
Use
slow
fast
Fast explores.
Slow remembers where the next valid element should go.
Move Zeroes
slow = 0
for fast in range(len(nums)):
if nums[fast] != 0:
nums[slow], nums[fast] = nums[fast], nums[slow]
slow += 1
Output
[1,3,12,0,0]
Interview frequency:
⭐⭐⭐⭐⭐
Pattern 3: Fast and Slow Pointer
Mostly used in Linked Lists.
slow
fast
slow -> 1 step
fast -> 2 steps
Applications
- Detect cycle
- Find middle node
- Happy Number
We'll revisit this when we study linked lists.
How to Identify Two Pointer Problems
Ask yourself:
Is the array sorted?
Example
Pair Sum
Use opposite pointers.
Do I need to compare two ends?
Palindrome
Reverse String
Use opposite pointers.
Am I moving/removing elements?
Move Zeroes
Remove Duplicates
Use slow and fast pointers.
Am I merging two sorted arrays?
Use one pointer for each array.
Common Interview Mistakes
Mistake 1
Using nested loops.
for i:
for j:
Many pair problems can be solved with two pointers in O(n) instead of O(n²).
Mistake 2
Using Two Pointers on an unsorted array when the problem depends on order.
For example:
Two Sum
If the array isn't sorted and you can't sort it (because you need original indices), use a Hash Map instead.
Mistake 3
Moving the wrong pointer.
Remember:
If the array is sorted:
Sum too small
Move left
Sum too big
Move right
Real Interview Questions
Master these:
- Valid Palindrome ⭐⭐⭐⭐⭐
- Reverse String ⭐⭐⭐⭐⭐
- Two Sum II ⭐⭐⭐⭐
- Move Zeroes ⭐⭐⭐⭐⭐
- Remove Duplicates from Sorted Array ⭐⭐⭐⭐⭐
- Squares of a Sorted Array ⭐⭐⭐⭐
- Container With Most Water ⭐⭐⭐⭐
- 3Sum ⭐⭐⭐⭐⭐ (advanced extension)
Interview Thinking Process
Suppose you're asked:
"Given a sorted array, find two numbers whose sum is
target."
A strong interview approach is:
- Notice the array is sorted.
- Recognise that brute force (
O(n²)) is unnecessary. - Suggest using two pointers (
left,right). - Explain why moving
leftincreases the sum and movingrightdecreases it. - Code the
O(n)solution.
This demonstrates both algorithmic knowledge and problem-solving, which is exactly what interviewers evaluate.
Next Topic
The next topic is Sliding Window, which is often confused with Two Pointers but solves a different class of problems such as:
- Longest substring without repeating characters
- Maximum sum subarray of size
k - Minimum window substring
- Longest repeating character replacement
Sliding Window is another ⭐⭐⭐⭐⭐ interview pattern and builds directly on the pointer concepts you've just learned.