How to Learn Patterns
- Recognize the pattern — Read the problem, identify which pattern applies
- Write the skeleton — Set up the data structure (map, two pointers, window)
- Fill in the logic — The invariant that defines your solution
- Verify with examples — Walk through with small inputs
Pattern Recognition Cheat Sheet
| Signal in Problem | Try This Pattern |
|---|---|
| "Find pair that sums to X" | Hash map or two pointers (if sorted) |
| "Count occurrences" | Hash map frequency counting |
| "Longest subarray with condition" | Sliding window |
| "Find in sorted data" | Binary search |
| "Matching/nesting" | Stack |
| "Minimum needed to satisfy" | Binary search on answer |
| "Merge sorted inputs" | Two pointers |
| "Next greater/smaller element" | Monotonic stack |
Interview Mindset
- State the pattern before coding: "This is a sliding window problem because..."
- Start with brute force, then optimize: "Brute force is O(n²), but with a map we get O(n)"
- Think out loud — interviewers care about your reasoning, not just the answer
- Test edge cases: empty input, single element, all same values