Advanced
Pattern Recognition Guide
This is your reference card. Every problem in this course maps to one of 8 core patterns. Learn to identify the pattern in the first 2 minutes of reading a problem, and you will solve it faster than someone who has memorized the solution but does not understand the pattern.
The Pattern Decision Tree
When you read a new problem, ask these questions in order:
Quick Decision Framework:
- Does the problem involve a sorted array or finding a pair/triplet? → Two Pointers or Binary Search
- Does it ask for a contiguous subarray/substring with some property? → Sliding Window
- Does it involve matching, nesting, or LIFO order? → Stack
- Does it ask for O(1) lookup or grouping by key? → Hash Map / Set
- Does it involve a tree or graph traversal? → BFS or DFS
- Does it ask for min/max of something with overlapping subproblems? → Dynamic Programming
- Does it ask for top k or k-th largest/smallest? → Heap
- Does it ask for an optimal schedule or assignment? → Greedy or Heap
Complete Problem-to-Pattern Map
Here is every problem in this course mapped to its primary pattern, with the key insight that unlocks the solution.
Pattern 1: Hash Map / Set
| # | Problem | Key Insight |
|---|---|---|
| 1 | Two Sum | Store complement in map; check before inserting |
| 10 | Contains Duplicate | Set membership check is O(1) |
| 15 | Group Anagrams | Sorted string or char count tuple as hash key |
| 18 | Subarray Sum Equals K | Prefix sum + hash map of frequencies |
Pattern 2: Two Pointers
| # | Problem | Key Insight |
|---|---|---|
| 3 | Merge Two Sorted Lists | Compare heads, advance the smaller one |
| 5 | Valid Palindrome | Compare from both ends, skip non-alphanumeric |
| 6 | Linked List Cycle | Fast/slow pointers meet if cycle exists |
| 11 | 3Sum | Fix one element, two pointers on sorted remainder |
| 12 | Container With Most Water | Start widest, shrink from shorter side |
| 43 | Trapping Rain Water | Track left_max and right_max from both ends |
Pattern 3: Sliding Window
| # | Problem | Key Insight |
|---|---|---|
| 13 | Longest Substring Without Repeating | Expand right, shrink left on duplicate |
| 47 | Sliding Window Maximum | Monotonic deque maintains decreasing order |
| 50 | Minimum Window Substring | Expand to satisfy, shrink to minimize |
Pattern 4: Stack
| # | Problem | Key Insight |
|---|---|---|
| 2 | Valid Parentheses | Push openers, pop and match closers |
| 37 | Min Stack | Auxiliary stack tracks min at each level |
| 48 | Largest Rectangle in Histogram | Monotonic increasing stack; pop on shorter bar |
Pattern 5: Tree / Graph Traversal (BFS & DFS)
| # | Problem | Key Insight |
|---|---|---|
| 8 | Binary Tree Inorder | Left-Root-Right with stack or recursion |
| 21 | Level Order Traversal | BFS with queue, process one level at a time |
| 22 | Validate BST | DFS with valid range (min, max) passed down |
| 23 | Number of Islands | DFS flood fill on each unvisited land cell |
| 24 | Course Schedule | Topological sort detects cycles in DAG |
| 25 | Word Search | Backtracking DFS with visited marking |
| 26 | Clone Graph | DFS/BFS + hash map from original to clone |
| 27 | Binary Tree Paths | DFS with path string building |
| 28 | Kth Smallest in BST | Inorder traversal gives sorted order; stop at k |
| 29 | Serialize/Deserialize Tree | Preorder with null markers for reconstruction |
| 30 | Lowest Common Ancestor | Recursive DFS; node is LCA if both subtrees return non-null |
| 46 | Word Ladder | BFS on implicit graph of one-letter changes |
Pattern 6: Dynamic Programming
| # | Problem | Key Insight |
|---|---|---|
| 7 | Climbing Stairs | dp[i] = dp[i-1] + dp[i-2] (Fibonacci variant) |
| 9 | Maximum Subarray | Kadane's: current_sum = max(num, current_sum + num) |
| 31 | Coin Change | dp[amount] = min coins; try each coin denomination |
| 32 | Word Break | dp[i] = can s[0:i] be segmented; check all splits |
| 33 | House Robber | dp[i] = max(skip, rob + dp[i-2]) |
| 34 | Unique Paths | dp[i][j] = dp[i-1][j] + dp[i][j-1] |
| 35 | Decode Ways | Like climbing stairs but with digit constraints |
| 44 | Longest Palindromic Substring | Expand around each center (not classic DP table) |
| 45 | Edit Distance | 2D DP: insert, delete, replace transitions |
| 49 | Regular Expression Matching | 2D DP: handle '.' and '*' transitions carefully |
Pattern 7: Binary Search
| # | Problem | Key Insight |
|---|---|---|
| 41 | Median of Two Sorted Arrays | Binary search on partition of shorter array |
Pattern 8: Heap / Greedy
| # | Problem | Key Insight |
|---|---|---|
| 38 | Top K Frequent Elements | Bucket sort by frequency or min heap of size k |
| 39 | Meeting Rooms II | Min heap of end times; reuse room if available |
| 40 | Task Scheduler | Greedy: most frequent task determines structure |
| 42 | Merge K Sorted Lists | Min heap of k list heads; always pop smallest |
Other Patterns
| # | Problem | Pattern | Key Insight |
|---|---|---|---|
| 4 | Best Time to Buy/Sell Stock | Track Min | Track minimum price seen; max profit = price - min |
| 14 | Product of Array Except Self | Prefix/Suffix | Left pass for prefixes, right pass for suffixes |
| 16 | Merge Intervals | Sort + Scan | Sort by start; merge if overlap with previous |
| 17 | Sort Colors | Three-way Partition | Dutch National Flag with three pointers |
| 19 | Rotate Array | Reverse | Reverse all, reverse first k, reverse rest |
| 20 | Spiral Matrix | Simulation | Shrink boundaries after each pass |
| 36 | LRU Cache | Design | Hash map + doubly linked list for O(1) operations |
How to Identify Patterns Quickly
Here are the trigger words and phrases that point to each pattern:
| Pattern | Trigger Words in Problem Statement |
|---|---|
| Hash Map | "find pair", "two numbers that", "group", "frequency", "count occurrences" |
| Two Pointers | "sorted array", "pair/triplet", "palindrome", "in-place", "cycle" |
| Sliding Window | "contiguous subarray", "substring", "window of size k", "longest/shortest" |
| Stack | "matching brackets", "nested", "next greater", "monotonic", "undo" |
| BFS/DFS | "shortest path", "connected components", "levels", "tree", "graph", "grid" |
| DP | "minimum/maximum", "number of ways", "can you reach", "subsequence", "partition" |
| Binary Search | "sorted", "find position", "minimize maximum", "O(log n)" |
| Heap | "top k", "k-th largest", "merge k", "schedule", "priority" |
Pattern Frequency at AI/ML Companies
Based on interview data, here is how frequently each pattern appears:
| Pattern | Frequency | Must-Know Problems |
|---|---|---|
| Hash Map / Set | Very High (25%) | Two Sum, Group Anagrams, Subarray Sum K |
| Tree / Graph | High (20%) | Number of Islands, Course Schedule, LCA |
| Dynamic Programming | High (18%) | Coin Change, Word Break, Edit Distance |
| Two Pointers | Medium (12%) | 3Sum, Trapping Rain Water |
| Sliding Window | Medium (8%) | Longest Substring, Minimum Window |
| Design | Medium (7%) | LRU Cache, Min Stack |
| Heap | Medium (6%) | Merge K Lists, Top K Frequent |
| Stack | Low (4%) | Valid Parentheses, Largest Rectangle |
Key Takeaways
- There are only 8 core patterns. Every problem is a variation of one (or occasionally two) patterns.
- Spend 2 minutes reading the problem and identifying the pattern before writing any code. This saves time in the long run.
- Hash maps and tree/graph traversal account for nearly half of all interview questions at AI/ML companies.
- When stuck, try the trigger word analysis: look for keywords in the problem statement that map to patterns.
- Print this page and use it as a reference card during practice sessions.
Lilly Tech Systems