Interview Pattern Mastery — The Complete Guide
📖 Concept
The 15 Essential Interview Patterns: Every coding interview problem maps to one or more of these patterns. Mastering pattern recognition is MORE valuable than solving 1000 random problems.
1. Two Pointers — Sorted array pair problems 2. Sliding Window — Substring/subarray with constraint 3. Fast & Slow Pointers — Cycle detection, middle finding 4. Binary Search — Sorted data, search on answer 5. BFS/DFS — Tree/graph traversal, shortest path 6. Backtracking — Generate permutations/combinations 7. Dynamic Programming — Optimal/count with overlapping sub-problems 8. Greedy — Local optimal → global optimal 9. Hash Map — Frequency counting, two sum, grouping 10. Stack/Queue — Parentheses, next greater, BFS 11. Heap (Priority Queue) — Top K, merge K sorted, median 12. Union-Find — Connected components, cycle detection 13. Trie — Prefix matching, word search 14. Monotonic Stack — Next greater/smaller, histogram 15. Prefix Sum — Range queries, subarray sums
Pattern → Problem Mapping Cheat Sheet:
- "Subarray/Substring with ___" → Sliding Window
- "Top K ___" → Heap
- "How many ways?" → DP
- "Find shortest ___" → BFS
- "Connected?" → Union-Find or DFS
- "Valid arrangement?" → Backtracking
- "Sorted array + pair" → Two Pointers
- "Prefix/starts with" → Trie
🏠 Real-world analogy: Patterns are like chess openings — you don't reinvent strategy each game. You recognize the position, apply the pattern, and adapt to the specific scenario.
💻 Code Example
1// PATTERN RECOGNITION EXAMPLES23// 1. "Find pair in sorted array" → TWO POINTERS4function twoSumSorted(arr, target) {5 let l = 0, r = arr.length - 1;6 while (l < r) {7 const sum = arr[l] + arr[r];8 if (sum === target) return [l, r];9 sum < target ? l++ : r--;10 }11 return [-1, -1];12}1314// 2. "Longest substring with at most K distinct" → SLIDING WINDOW15function longestKDistinct(s, k) {16 const freq = new Map();17 let maxLen = 0, left = 0;18 for (let right = 0; right < s.length; right++) {19 freq.set(s[right], (freq.get(s[right]) || 0) + 1);20 while (freq.size > k) {21 const c = s[left];22 freq.set(c, freq.get(c) - 1);23 if (freq.get(c) === 0) freq.delete(c);24 left++;25 }26 maxLen = Math.max(maxLen, right - left + 1);27 }28 return maxLen;29}3031// 3. "Minimum cost to ___" → DYNAMIC PROGRAMMING32function minCostPath(grid) {33 const m = grid.length, n = grid[0].length;34 for (let i = 0; i < m; i++)35 for (let j = 0; j < n; j++) {36 if (i === 0 && j === 0) continue;37 const up = i > 0 ? grid[i-1][j] : Infinity;38 const left = j > 0 ? grid[i][j-1] : Infinity;39 grid[i][j] += Math.min(up, left);40 }41 return grid[m-1][n-1];42}4344// 4. "Find connected components" → UNION-FIND45function countComponents(n, edges) {46 const parent = Array.from({length: n}, (_, i) => i);47 const find = (x) => parent[x] === x ? x : (parent[x] = find(parent[x]));48 const union = (a, b) => { parent[find(a)] = find(b); };49 for (const [a, b] of edges) union(a, b);50 return new Set(Array.from({length: n}, (_, i) => find(i))).size;51}5253// 5. "Generate all valid ___" → BACKTRACKING54function generateParentheses(n) {55 const result = [];56 function bt(s, o, c) {57 if (s.length === 2 * n) { result.push(s); return; }58 if (o < n) bt(s + '(', o + 1, c);59 if (c < o) bt(s + ')', o, c + 1);60 }61 bt('', 0, 0);62 return result;63}6465// INTERVIEW DECISION TREE66function choosePattern(problem) {67 const keywords = problem.toLowerCase();68 if (keywords.includes('sorted') && keywords.includes('pair')) return 'Two Pointers';69 if (keywords.includes('substring') || keywords.includes('subarray')) return 'Sliding Window';70 if (keywords.includes('top k') || keywords.includes('kth')) return 'Heap';71 if (keywords.includes('shortest path')) return 'BFS';72 if (keywords.includes('how many ways') || keywords.includes('minimum cost')) return 'DP';73 if (keywords.includes('generate all') || keywords.includes('permutation')) return 'Backtracking';74 if (keywords.includes('connected')) return 'Union-Find / DFS';75 if (keywords.includes('prefix') || keywords.includes('starts with')) return 'Trie';76 return 'Hash Map (default fallback)';77}
🏋️ Practice Exercise
Practice Challenge: Identify the Pattern:
- "Find the longest substring without repeating characters" → ?
- "Find the minimum window containing all characters of t" → ?
- "Count the number of islands" → ?
- "Find if a path exists between two nodes" → ?
- "Top K frequent elements" → ?
- "Generate all subsets of a set" → ?
- "Coin change — minimum coins" → ?
- "Next greater element" → ?
- "Validate BST" → ?
- "Course schedule — can finish all courses?" → ?
Answers: 1) Sliding Window 2) Sliding Window 3) DFS/BFS 4) BFS/DFS 5) Heap 6) Backtracking 7) DP 8) Monotonic Stack 9) DFS with range 10) Topological Sort
⚠️ Common Mistakes
Trying to memorize solutions instead of learning patterns — you'll fail on any variation
Not starting with brute force — always discuss brute force first, then optimize
Jumping to code without discussing approach — interviewers want to see your thinking process
Not considering multiple patterns — some problems can be solved with different approaches
Ignoring time/space complexity trade-offs — always discuss both
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Interview Pattern Mastery — The Complete Guide