17 FAANG Patterns
Pattern 1: Two Pointers
Mark when done:
Deep Dive: Pointer Movement Visualized
Opposite Direction -- Two Sum II (Sorted)
nums = [2, 7, 11, 15], target = 9:
L=0 R=3 sum = 2+15 = 17 > 9 -> R--
L=0 R=2 sum = 2+11 = 13 > 9 -> R--
L=0 R=1 sum = 2+7 = 9 == 9 -> FOUND [L, R]
Why this works on sorted data: Sum too large? The only way to shrink it is move R left (smaller number). Sum too small? Move L right (bigger number). Each step eliminates an entire row/column of the pair matrix.
The Pair Matrix Insight
For sorted [1, 3, 5, 7] with target 8:
1 3 5 7
1 2 4 6 8* <- found (1,7)
3 6 8* 10 <- found (3,5)
5 10 12
7 14
Two pointers walk the anti-diagonal. O(n) vs O(n^2) brute.
Signal: Sorted input, find pair/triplet, palindrome check, partitioning
Template: two_pointers_opposite() in templates.py
When to Use
- Input is sorted (or you sort it first)
- Find pair/triplet that satisfies a condition
- In-place partitioning with O(1) space
- Palindrome check (L and R from both ends)
Re-solve from Phase 1-2 (TIMED)
| Problem | Source | Target Time | Done? |
|---|---|---|---|
| 3Sum | Phase 2.2 | 20 min | [ ] |
| Container With Most Water | Phase 2.2 | 15 min | [ ] |
| Valid Palindrome | Phase 2.2 | 10 min | [ ] |
New Practice Problems
Add problems to solutions/ and use the enrich-solutions prompt to generate placeholders.
First-time Recognition Signals
When you read a brand-new problem statement, this pattern is the right tool if you see:
- "Sorted array, find a pair / triplet summing to T" — the sortedness gives a meaningful direction to move pointers (smaller / larger). Each pointer step eliminates an entire row or column of the pair matrix.
- "Find a pair satisfying a condition in an array" when the array is (or can be) sorted and a HashMap is overkill — points at left/right convergence.
- "In-place modification preserving relative order" (Move Zeros, Remove Duplicates, partition by predicate) — same-direction (slow / fast) two pointers do this in one pass with O(1) extra space.
- "Longest / shortest subarray with property X over a sorted input" — opposite-direction two pointers, expanding or contracting based on the predicate.
- "Palindrome check / reverse half a string / partition around a value" — natural left-and-right convergence on the same array.
Anti-signals (looks like this pattern, isn't)
- "Subarray sum equals K" with negative numbers in the array — looks like a window/two-pointer problem, but is actually Prefix Sum + HashMap (negatives break the monotonic shrink).
- "Find pair summing to T in unsorted array" when sorting would change the answer (e.g. you must return original indices) — use HashMap, not Two Pointers.
- "K-closest pairs from two arrays" — sounds like two pointers walking both arrays, but is really a Heap problem (Top-K).