Pattern: Two Pointers & Fast-Slow Pointers
πΉ Why This Pattern?
Two Pointers (and its fast-slow variant) is one of the most frequently tested patterns across FAANG interviews. It appears in:
- Arrays β sorted arrays, pair-sums, container problems.
- Strings β palindrome checks, valid substring problems.
- Linked Lists β cycle detection, middle node, removing N-th node.
- Intervals β merging, intersecting timelines.
It is optimal in problems where brute force = O(nΒ²), but two pointers optimize to O(n).
π₯ Sub-Patterns
1οΈβ£ Opposite-End Pointers (Left-Right Shrink)
Best for sorted arrays and string palindrome checks.
Example: Two Sum II β Input Sorted Array (Amazon/Apple)
Find two numbers in a sorted array that sum up to target.
class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0, right = numbers.length - 1;
while (left < right) {
int sum = numbers[left] + numbers[right];
if (sum == target) {
return new int[]{left + 1, right + 1}; // 1-indexed
} else if (sum < target) {
left++;
} else {
right--;
}
}
return new int[]{-1, -1};
}
}
Complexity: O(n) time, O(1) space.
2οΈβ£ Fast & Slow Pointers (Linked List Magic)
Used for cycle detection, finding middle, or removing nth node.
Example: Detect Cycle in Linked List (Google/Amazon)
Classic Floydβs Cycle Detection (Tortoise and Hare).
class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
3οΈβ£ Sliding Two Pointers (Shrink/Grow Window)
Used in strings/arrays where we shrink/grow a window.
Example: Container With Most Water (Meta/Apple)
class Solution {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int max = 0;
while (left < right) {
int area = (right - left) * Math.min(height[left], height[right]);
max = Math.max(max, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max;
}
}
π Flagship Problems by Company
| Problem | Company | Pattern | Difficulty |
|---|---|---|---|
| Linked List Cycle | Google, Amazon | Fast-Slow | Easy |
| Middle of Linked List | Fast-Slow | Easy | |
| Remove N-th Node from End | Amazon, Meta | Fast-Slow | Medium |
| Two Sum II (Sorted) | Amazon, Apple | Opposite-End | Easy |
| Valid Palindrome II | Opposite-End | Easy/Medium | |
| 3Sum | Meta, Amazon | Opposite-End + Sorting | Medium |
| Container With Most Water | Meta, Apple | Opposite-End | Medium |
| Trapping Rain Water (2-pointer version) | Apple, Meta | Opposite-End | Hard |
| Interval Intersection | Netflix | Two Pointers | Medium |
π§ Quick βPattern Recognitionβ Checklist
- Array sorted? β Think two pointers from both ends.
- Linked list cycle/middle/removal? β Fast/Slow pointers.
- Palindrome/string check? β Left/right pointer shrink.
- Interval problems? β Two pointers across both lists.
π― Practice Set (Homework with Hints)
Linked List Cycle II (Google/Amazon) β Return cycle start node.
Hint: After meeting point, reset one pointer to head.Remove Nth Node from End of List (Amazon/Meta) β Use fast pointer
nahead.Valid Palindrome II (Google) β Allow one mismatch and retry by skipping either side.
3Sum Closest (Meta) β Sort array, fix one, use two pointers inside.
Trapping Rain Water (Two Pointers) (Apple/Meta) β Maintain leftMax, rightMax.
π Key Takeaway
Two pointers are like a Swiss Army Knife for FAANG interviews.
If you see sorted arrays, linked lists, palindromes, or intervals, 90% chance the solution involves this pattern.
π
Coming Up β Day 3:
π Prefix Sum + Difference Arrays + Hybrid Patterns (very Google/Amazon heavy).
Top comments (0)