Pattern #5
Two Pointers
Important interview questions, thinking patterns, pointer types, and rules for solving two pointer problems in Go.
Must Solve
15 core questions — solve these first.
- 1.Valid PalindromeLeft + Righteasy
- 2.Two Sum II - Input Array Is SortedLeft + Rightmedium
- 3.Remove Duplicates from Sorted ArraySlow + Fasteasy
- 4.Remove ElementRead + Writeeasy
- 5.Move ZeroesSlow + Fasteasy
- 6.Squares of a Sorted ArrayLeft + Righteasy
- 7.Reverse StringLeft + Righteasy
- 8.Reverse Words in a StringReverse + Two Pointersmedium
- 9.Container With Most WaterLeft + Rightmedium
- 10.3SumSort + Fix + Two Pointersmedium
- 11.3Sum ClosestSort + Fix + Two Pointersmedium
- 12.Sort ColorsDutch National Flagmedium
- 13.Backspace String CompareReverse scaneasy
- 14.Trapping Rain WaterLeft + Righthard
- 15.Boats to Save PeopleSort + Left + Rightmedium
Also Important
10 more questions worth practicing.
- 16.Merge Sorted ArrayTwo pointers from endeasy
- 17.Intersection of Two ArraysSort + Two pointerseasy
- 18.Is SubsequenceTwo pointers on botheasy
- 19.Valid Palindrome IILeft + Right + skipeasy
- 20.Partition LabelsGreedy + Two pointersmedium
- 21.Minimum Size Subarray SumSliding window variantmedium
- 22.4SumSort + Fix two + Two Pointersmedium
- 23.Dutch National Flag ProblemThree pointersmedium
- 24.Remove Nth Node From End of ListFast + Slow gapmedium
- 25.Linked List CycleFast + Sloweasy
How to Think
- Array/string is sorted?Two Pointers
- Need pair with target?left + right
- Need compare from both ends?left → ← right
- Need remove/move in-place?read pointer + write pointer
- Need compare every pair?Sort + Two Pointers → O(n log n)
- Need triplets?Sort + Fix one + Two Pointers
- Need O(1) extra space?Try in-place Two Pointers
Go Quick Reference
Two Sum II — Sorted (Opposite Direction)
func twoSum(nums []int, target int) []int {
lo, hi := 0, len(nums)-1
for lo < hi {
sum := nums[lo] + nums[hi]
if sum == target {
return []int{lo + 1, hi + 1}
} else if sum < target {
lo++
} else {
hi--
}
}
return nil
}👉 Time: O(n) | Space: O(1)
Move Zeroes (Same Direction — Slow + Fast)
func moveZeroes(nums []int) {
slow := 0
for fast := 0; fast < len(nums); fast++ {
if nums[fast] != 0 {
nums[slow], nums[fast] = nums[fast], nums[slow]
slow++
}
}
}👉 Time: O(n) | Space: O(1)
Container With Most Water (Opposite Direction)
func maxArea(height []int) int {
lo, hi := 0, len(height)-1
best := 0
for lo < hi {
w := hi - lo
h := min(height[lo], height[hi])
if area := w * h; area > best {
best = area
}
if height[lo] < height[hi] {
lo++
} else {
hi--
}
}
return best
}👉 Time: O(n) | Space: O(1)
3Sum (Sort + Fix one + Two Pointers)
func threeSum(nums []int) [][]int {
sort.Ints(nums)
var res [][]int
for i := 0; i < len(nums)-2; i++ {
if i > 0 && nums[i] == nums[i-1] { continue }
lo, hi := i+1, len(nums)-1
for lo < hi {
sum := nums[i] + nums[lo] + nums[hi]
if sum == 0 {
res = append(res, []int{nums[i], nums[lo], nums[hi]})
for lo < hi && nums[lo] == nums[lo+1] { lo++ }
for lo < hi && nums[hi] == nums[hi-1] { hi-- }
lo++; hi--
} else if sum < 0 { lo++ } else { hi-- }
}
}
return res
}👉 Time: O(n²) | Space: O(1)
Main Two Pointer Types
1. Opposite Direction
left → [........] ← rightUsed for: Palindrome, Two Sum Sorted, Container With Most Water, 3Sum, Trapping Rain Water
2. Same Direction
slow →
fast ---->Used for: Move Zeroes, Remove Duplicates, Remove Element, In-place modifications
3. Two Different Arrays
i → array1
j → array2Used for: Merge Sorted Array, Intersection, Comparing sorted data
Interview Rules
- 1. Sorted? → Think Two Pointers
- 2. Pair target? → Left + Right
- 3. Palindrome? → Both ends
- 4. Remove duplicates? → Slow + Fast
- 5. Move items? → Read + Write
- 6. Merge sorted arrays? → Pointer on each
- 7. Triplets? → Sort + Fix one + Two Pointers
- 8. Need O(1) extra space? → Try in-place Two Pointers
Small Rules
- Rule 1: Sorted array is a big hint. Think
Two PointersorBinary Search. - Rule 2: If both pointers move only forward → Usually
O(n), notO(n²). Even with inner loop. - Rule 3: Move the pointer that can improve the answer.
sum < target → left++ - Rule 4: Two pointers often save extra memory. Modify original array → possible
O(1)space.
Production Thinking
Merge sorted data → pointer A + pointer B
Remove invalid items → read pointer + write pointer
Compare from both sides → left + right
Memory matters (large arrays) → O(1) extra space
Remember This
Sorted + Pair → Two Pointers
Palindrome → Left + Right
Remove/Move → Slow + Fast
Merge sorted data → Pointer on both
3Sum → Sort + Two Pointers
In-place → Two Pointers💡 Two Pointers is one of the highest ROI patterns. Master these 25 problems and you can handle most pointer-based interview questions.