When to Use What
Use this page when you see a new problem and need to identify the right pattern. Start at the top of the decision tree and follow the branches.
Master Decision Tree
Problem Type Quick Reference
Arrays & Hashing
| When You See... | Use This | Implementation |
|---|---|---|
| "Find two values that sum to X" | Hash map lookup | arrays/two_sum.py |
| "Group items by property" | Hash map grouping | arrays/group_anagrams.py |
| "Top K / most frequent" | Bucket sort or heap | arrays/top_k_frequent.py |
| "Product without self" | Prefix/suffix arrays | arrays/product_except_self.py |
Two Pointers
| When You See... | Use This | Implementation |
|---|---|---|
| "Three values sum to zero" | Sort + two pointers | two_pointers/three_sum.py |
| "Maximum area / container" | Converging pointers | two_pointers/container_with_most_water.py |
| "Water trapped between bars" | Pointers + max tracking | two_pointers/trapping_rain_water.py |
Sliding Window
| When You See... | Use This | Implementation |
|---|---|---|
| "Minimum window containing chars" | Variable sliding window | sliding_window/min_window_substring.py |
| "Longest substring without repeat" | Sliding window + hash | sliding_window/longest_substring_no_repeat.py |
Stacks & Queues
| When You See... | Use This | Implementation |
|---|---|---|
| "Valid brackets/parentheses" | Stack matching | stacks_queues/valid_parentheses.py |
| "Min/max in O(1)" | Auxiliary stack | stacks_queues/min_stack.py |
| "Next greater/warmer element" | Monotonic stack | stacks_queues/daily_temperatures.py |
Graphs
| When You See... | Use This | Implementation |
|---|---|---|
| "Connected components / islands" | BFS/DFS flood fill | graphs/number_of_islands.py |
| "Dependency ordering" | Topological sort | graphs/topological_sort.py |
| "Shortest path (positive)" | Dijkstra | graphs/dijkstra.py |
| "Shortest path (negative)" | Bellman-Ford | graphs/bellman_ford.py |
| "Pathfinding with heuristic" | A* search | graphs/a_star_search.py |
| "Minimum cost to connect all" | Kruskal's MST | graphs/minimum_spanning_tree.py |
| "Maximum flow" | Edmonds-Karp | graphs/network_flow.py |
| "Spatial proximity" | Geohash / KD-tree | graphs/geohash_grid.py, graphs/kd_tree.py |
Dynamic Programming
| When You See... | Use This | Implementation |
|---|---|---|
| "Count ways to reach target" | Fibonacci-like DP | dp/climbing_stairs.py |
| "Minimum cost to reach amount" | Bottom-up DP | dp/coin_change.py |
| "Longest increasing subsequence" | DP + binary search | dp/longest_increasing_subseq.py |
| "String edit distance" | 2D DP | dp/edit_distance.py |
| "0/1 Knapsack" | 1D optimized DP | dp/knapsack.py |
| "Visit all cities" | Bitmask DP (TSP) | dp/traveling_salesman_dp.py |
Backtracking
| When You See... | Use This | Implementation |
|---|---|---|
| "Generate all subsets" | Backtracking | backtracking/subsets.py |
| "Generate all permutations" | Backtracking | backtracking/permutations.py |
| "Combinations summing to target" | Backtracking w/ reuse | backtracking/combination_sum.py |
| "N queens placement" | Backtracking + pruning | backtracking/n_queens.py |
Heaps & Greedy
| When You See... | Use This | Implementation |
|---|---|---|
| "Kth largest element" | Min-heap of size k | heaps/kth_largest.py |
| "Merge k sorted lists" | Heap merge | heaps/merge_k_sorted_lists.py |
| "Task scheduling" | Max-heap + queue | heaps/task_scheduler.py |
| "Merge overlapping intervals" | Sort + merge | greedy/merge_intervals.py |
| "Can reach end? Min jumps?" | Greedy max-reach | greedy/jump_game.py |
| "Max non-overlapping intervals" | Greedy by end time | greedy/interval_scheduling.py |
Pattern Recognition Keywords
When you read a problem statement, look for these keywords and jump to the right approach.
| Keyword | First Thing to Try | Fallback |
|---|---|---|
| "sorted" | Binary search, two pointers | -- |
| "contiguous subarray" | Sliding window | Prefix sums, Kadane's |
| "substring" | Sliding window + hash map | DP |
| "parentheses" / "brackets" | Stack | -- |
| "next greater" / "next warmer" | Monotonic stack | -- |
| "shortest path" | BFS (unweighted), Dijkstra (weighted) | Bellman-Ford, A* |
| "connected" / "island" / "region" | DFS/BFS flood fill, Union-Find | -- |
| "dependency" / "prerequisite" | Topological sort | -- |
| "cycle" | DFS coloring (directed), Union-Find (undirected) | -- |
| "minimum cost" / "count ways" | Dynamic programming | -- |
| "all subsets" / "all combinations" | Backtracking | Bitmask enumeration |
| "merge" / "overlapping intervals" | Sort + greedy sweep | -- |
| "kth largest" / "top k" | Heap of size k | Quickselect |
| "design" / "implement" | Choose data structures, define API | -- |
| "cache" / "eviction" | Hash map + doubly linked list (LRU) | -- |
| "stream" / "online" | Heap, sliding window | -- |
| "frequency" / "how many times" | Hash map / Counter | -- |
| "edit distance" / "transform" | 2D DP | BFS (word ladder) |
| "knapsack" / "subset sum" | DP (1D or 2D) | -- |
| "schedule tasks" | Heap + greedy | Topological sort if deps |
| "maximum flow" | Edmonds-Karp / Ford-Fulkerson | -- |
| "nearest point" / "proximity" | KD-tree, geohash | -- |
| "visit all cities/nodes" | TSP bitmask DP | -- |
| "XOR" / "single unique" | Bit manipulation | -- |
Data Structure Selection
Need key -> value? --> dict
Need "is X in the set?" --> set
Need min/max repeatedly? --> heapq
Need FIFO? --> collections.deque
Need LIFO? --> list (append/pop)
Need sorted insert+search --> bisect / SortedList
Need merge/find groups? --> Union-Find
Need nearest in 2D/3D? --> KD-tree or geohash
This page lives in git. Anyone can propose an edit. Edit this page View source