SLIDE 104: Section Intro, Pattern Recognition and Cheat Sheets

Section 6: Pattern Recognition and Cheat Sheets

You now know every major pattern in this course: tree traversal, graph search, heaps, intervals, greedy, backtracking, and dynamic programming. The final skill is putting it all together. Given an unfamiliar problem, how do you identify which pattern applies within the first 60 seconds?

This section consolidates everything into a single decision framework, gives you three rounds of mixed-pattern practice, and provides cheat sheets for quick revision.

What We’ll Cover:

  1. The pattern decision tree
  2. Signal-to-pattern mapping
  3. Complexity reference across all patterns
  4. Mixed-pattern interview practice (3 rounds)
  5. When patterns combine
  6. Common mistakes across all sections
  7. Per-section cheat sheets
  8. What to practice next

SLIDE 105: The Pattern Decision Tree

How to Classify a Problem in 60 Seconds:

START: Read the problem statement.

Is the input a TREE?
├── Need level-by-level processing? → BFS (Slide 10)
├── Need subtree results (height, diameter)? → DFS postorder (Slide 5)
├── BST? → Exploit sorted-inorder property (Slide 12)
├── Path from root to leaf? → Pass value downward (Slide 16)
└── Find common ancestor? → LCA pattern (Slide 17)

Is the input a GRAPH or GRID?
├── Shortest path (unweighted)? → BFS
├── Connected components or regions? → DFS or BFS
├── Spreading from multiple sources? → Multi-source BFS (Slide 31)
├── Dependencies or prerequisites? → Topological sort (Slide 35)
├── Dynamic grouping/merging? → Union-Find (Slide 38)
└── Cycle detection (directed)? → Three-color DFS (Slide 33)

Does the problem need TOP K or REPEATED MIN/MAX?
├── Yes → Heap (Slide 47)
└── Merge K sorted sequences? → K-way merge (Slide 50)

Is the problem about OVERLAPPING TIME RANGES?
├── Merge overlapping intervals? → Sort by start (Slide 54)
├── Maximum concurrent / minimum rooms? → Event splitting (Slide 55)
└── Remove minimum to avoid conflicts? → Sort by end, greedy (Slide 56)

Does the problem ask for ALL COMBINATIONS/PERMUTATIONS?
├── All subsets? → Backtracking, record at every node (Slide 68)
├── All permutations? → Backtracking with used array (Slide 69)
├── Combinations that sum to target? → Backtracking with pruning (Slide 70)
└── Find a word/path in a grid? → Grid backtracking (Slide 73)

Does the problem ask for COUNT, MIN, or MAX with overlapping subproblems?
├── State is one variable (index or amount)? → 1D DP (Slide 85)
├── State is a grid position? → 2D Grid DP (Slide 89)
├── State is two string positions? → 2D String DP (Slide 92)
├── "For each i, check all j < i"? → Sequence DP (Slide 94)
├── Include or exclude items under capacity? → Knapsack DP (Slide 96)
└── Can a local choice be proven optimal? → Greedy instead (Slide 58)

Interview Relevance: Run through this decision tree mentally before writing any code. It takes 30 seconds and prevents the most common mistake: picking the wrong pattern and wasting 15 minutes before restarting.