Loading…
Loading…
Charting your lesson…
Opening your lesson…
Two-Pointer Technique
Lesson 2 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Two pointers is the single highest-yield array pattern in coding interviews. It repeatedly turns an obvious O(n²) brute force into a clean O(n) scan by walking two indices instead of nesting two loops.
Core idea: keep two indices into the array and move them under a rule that never revisits a discarded possibility. Because each pointer advances at most n times, the whole scan is linear.
Step 2/ 61 min
left = 0, right = n-1, move inward. Ideal on a sorted array or when comparing a pair from both sides (two-sum, palindrome check, container-with-most-water).Two-Sum on a sorted array: if a[left] + a[right] is too small, the smallest element can never pair with anything smaller than a[right], so left++ is safe. If too big, right--. Every move eliminates one element permanently, so at most n moves → O(n) time, O(1) space (versus O(n²) brute force or O(n) with a hash set that costs extra space).
left, right = 0, n-1
while left < right:
s = a[left] + a[right]
if s == target: return (left, right)
elif s < target: left += 1
else: right -= 1
"Remove all zeros, keep order" uses a write pointer that lags a read pointer:
write = 0
for read in 0..n-1:
if a[read] != 0:
a[write] = a[read]; write += 1
Read visits every slot once; write only advances on a keeper. O(n) time, O(1) space, and stable.
Step 3/ 61 min
Is "A man a plan a canal Panama" a palindrome (letters only)? Point left and right at the ends. Skip non-letters, lowercase, compare. Each character is visited once → O(n). They meet in the middle having matched every pair → palindrome. Brute-forcing by building the reverse string costs O(n) extra space; two pointers cost none.
Step 4/ 61 min
Signal words: sorted array, pair/triplet, palindrome, "in place", remove/partition, reverse. If a nested loop compares a[i] with a[j] where j only ever needs to move one way as i moves, two pointers collapse it to O(n). Triplet problems (3-Sum) fix one index and two-pointer the rest → O(n²) instead of O(n³).
Step 5/ 61 min
left < right (pairs) or left <= right (searching a target).Using converging two pointers on an unsorted array for two-sum. The "too small → move left" logic depends on order; on unsorted data it silently skips valid pairs. Sort first, or use a hash set instead.
Step 6/ 6· Worked examples1 min
Problem
In the sorted array [1, 3, 4, 6, 8, 10], find a pair that sums to 12 using two pointers.
Try it on paper first.
Recall
Keep this
The two-pointer technique is one of the highest-yield patterns for array and string interviews: it turns many O(n²) brute-force scans into a single O(n) pass with O(1) extra space. Whenever you catch yourself writing a nested loop over the same array, ask "can two pointers replace the inner loop?"
Keep two indices and move them under a rule instead of re-scanning. Three flavours dominate:
For opposite-ends pair-sum: if arr[l]+arr[r] is too small move l right, too large move r left. Each pointer moves at most n times → O(n).
| Variant | Time | Space | Precondition |
|---|---|---|---|
| Opposite-ends pair sum | O(n) | O(1) | sorted |
| Fast-slow in-place filter | O(n) | O(1) | none |
| Merge two sorted arrays | O(n+m) | O(1) | both sorted |
| Sort then two-pointer | O(n log n) | O(1) | sortable |
"Find pair/triplet with target sum", "remove duplicates in place", "reverse/partition", or "container with most water". Interviewers expect you to justify why moving a specific pointer is safe (monotonicity) and to state O(n)/O(1).
Two pointers replace a nested loop: opposite ends on sorted data, fast-slow for in-place edits, or dual-sequence merge. Reduces O(n²)→O(n) with O(1) space — always argue why the pointer move is correct.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.