Opposite or parallel pointers traversing sequences in linear time O(N)
225 problems·62 Easy·132 Medium·31 Hard
Pattern Study Guide & Cheat Sheet· O(n) [or O(n log n) if sorting is required] · O(1) auxiliary memory
▼
Search pairs or reverse sequences by moving two indices inward or in parallel, pruning suboptimal options in O(1) time without nested loops.
Core Invariant: Maintain the search space invariant: elements outside [left, right] have already been evaluated and eliminated. Advance whichever pointer monotonically improves or resolves the target condition.
Recognize it (Keywords & Signals)
Array is sorted and you need to find a pair, triplet, or target sum
Palindrome verification, string reversal, or inward symmetry checking
Partitioning an array around a pivot (Dutch National Flag, sort colors)
Trapping rainwater / container boundaries where width and height constrain area
When NOT to use
Array is unsorted and problem requires contiguous subarrays with sum K (especially with negative numbers; use Prefix Sum + Hash Map instead).
How to solve (Step-by-step)
1.Initialize left = 0 and right = len(nums) - 1 (for opposite-direction pointers).
2.In a `while left < right` loop, evaluate the current state: curr_sum = nums[left] + nums[right].
3.If target matched, record answer or return immediately.
4.If curr_sum < target, advance `left += 1` to increase sum; if curr_sum > target, decrement `right -= 1` to decrease sum.
5.Skip duplicate values using inner while loops when unique tuples are required (e.g. 3Sum).
Watch for (Interview Traps)
Off-by-one in loop condition (`left < right` vs `left <= right` when element reuse is prohibited)
Forgetting to skip duplicate elements inside the loop when unique triplets/quadruplets are needed
Not advancing pointers inside duplicate skip loops causing infinite loops
Opposite-Direction Two Pointers
def two_pointers_search(nums: list[int], target: int) -> list[int]:
# Prerequisite: array must be sorted
left, right = 0, len(nums) - 1
while left < right:
curr_sum = nums[left] + nums[right]
if curr_sum == target:
return [left, right]
elif curr_sum < target:
left += 1 # Need larger sum -> move left inward
else:
right -= 1 # Need smaller sum -> move right inward
return []
Cost
O(n) [or O(n log n) if sorting is required] · O(1) auxiliary memory (Sort in-place to preserve O(1) space guarantee.)
Canonical problems
#167 Two Sum II - Input Array Is Sorted:Inward movement directly targets sum
#15 3Sum:Fix one element, then run Two Pointers on sorted remainder
#11 Container With Most Water:Always move the shorter line inward
#125 Valid Palindrome:Compare characters inward while skipping non-alphanumerics
What is the Two Pointers pattern in coding interviews?
Opposite or parallel pointers traversing sequences in linear time O(N) It is categorized under Fundamentals and features 225 curated interview problems across Easy (62), Medium (132), and Hard (31) difficulties.
Which tech companies frequently test Two Pointers?
Companies frequently asking Two Pointers questions include Google, Amazon, Meta, Microsoft, Bloomberg, TikTok.
How do I master the Two Pointers pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.