When to Use What -- Decision Tree
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
graph TD
START["What does the problem ask for?"]
START --> FIND["Find / search for something"]
START --> OPTIMIZE["Optimize a value<br/>(min cost, max profit, count ways)"]
START --> GENERATE["Generate all / enumerate"]
START --> SEQUENCE["Process a sequence<br/>(subarray, substring)"]
START --> GRAPH["Graph structure<br/>(explicit or implicit)"]
START --> DESIGN["Design a data structure"]
%% FIND branch
FIND --> SORTED{"Input is sorted?"}
SORTED -->|YES| BSEARCH["Binary Search<br/>searching/binary_search.py"]
SORTED -->|"Rotated?"| ROTATED["search_rotated_array.py<br/>find_minimum_rotated.py"]
SORTED -->|NO| HASHMAP["Hash Map O(n) lookup<br/>arrays/two_sum.py"]
FIND --> FINDGRAPH{"Find in a graph?"}
FINDGRAPH -->|Unweighted| BFS["BFS<br/>graphs/number_of_islands.py"]
FINDGRAPH -->|"Weighted +"| DIJKSTRA["Dijkstra<br/>graphs/dijkstra.py"]
FINDGRAPH -->|"Weighted -"| BELLMAN["Bellman-Ford<br/>graphs/bellman_ford.py"]
FINDGRAPH -->|Heuristic| ASTAR["A* Search<br/>graphs/a_star_search.py"]
%% OPTIMIZE branch
OPTIMIZE --> OVERLAP{"Overlapping<br/>subproblems?"}
OVERLAP -->|YES| DP["Dynamic Programming"]
DP --> DP1D["1D: climbing_stairs, coin_change"]
DP --> DP2D["2D: edit_distance, longest_common_subseq"]
DP --> DPBIT["Bitmask: traveling_salesman_dp"]
OVERLAP -->|NO| GREEDY["Greedy<br/>merge_intervals, jump_game,<br/>interval_scheduling"]
OPTIMIZE --> TOPK{"Top-K or<br/>priority ordering?"}
TOPK -->|"Kth element"| HEAP["Heap<br/>heaps/kth_largest.py"]
TOPK -->|"Merge K"| HEAPMERGE["Heap Merge<br/>heaps/merge_k_sorted_lists.py"]
%% GENERATE branch
GENERATE --> SUBSETS["All subsets<br/>backtracking/subsets.py"]
GENERATE --> PERMS["All permutations<br/>backtracking/permutations.py"]
GENERATE --> COMBSUM["Combinations to sum<br/>backtracking/combination_sum.py"]
GENERATE --> PLACEMENT["Placement with rules<br/>backtracking/n_queens.py"]
%% SEQUENCE branch
SEQUENCE --> CONTIG{"Contiguous?"}
CONTIG -->|YES| WINDOW["Sliding Window"]
WINDOW --> FIXEDWIN["Fixed size: standard pattern"]
WINDOW --> VARWIN["Variable: min_window_substring,<br/>longest_substring_no_repeat"]
CONTIG -->|NO| SUBSEQ["Subsequence DP<br/>longest_increasing_subseq,<br/>longest_common_subseq"]
SEQUENCE --> NEXTGT["Next greater/smaller?<br/>Monotonic Stack<br/>stacks_queues/daily_temperatures.py"]
SEQUENCE --> NESTING["Valid nesting?<br/>Stack<br/>stacks_queues/valid_parentheses.py"]
%% GRAPH branch
GRAPH --> COMPONENTS["Connected components<br/>DFS/BFS or Union-Find<br/>graphs/number_of_islands.py"]
GRAPH --> DEPORDER["Dependency ordering<br/>Topological Sort<br/>graphs/topological_sort.py"]
GRAPH --> CYCLE["Cycle detection<br/>graphs/course_schedule.py"]
GRAPH --> CLONE["Clone / copy<br/>graphs/clone_graph.py"]
GRAPH --> MST["Min spanning tree<br/>graphs/minimum_spanning_tree.py"]
GRAPH --> MAXFLOW["Maximum flow<br/>graphs/network_flow.py"]
%% DESIGN branch
DESIGN --> LRU["O(1) access + eviction<br/>linked_lists/lru_cache.py"]
DESIGN --> MINSTACK["O(1) min/max retrieval<br/>stacks_queues/min_stack.py"]
DESIGN --> SORTEDDS["Sorted insert + search<br/>bisect / SortedList"]
DESIGN --> GENERIC["Generic typed container<br/>concepts/advanced_typing.py"]
%% Styling
classDef start fill:#6366f1,color:#fff,stroke:#4f46e5
classDef branch fill:#f59e0b,color:#000,stroke:#d97706
classDef leaf fill:#10b981,color:#fff,stroke:#059669
classDef decision fill:#fff,color:#000,stroke:#6b7280
class START start
class FIND,OPTIMIZE,GENERATE,SEQUENCE,GRAPH,DESIGN branch
class BSEARCH,ROTATED,HASHMAP,BFS,DIJKSTRA,BELLMAN,ASTAR leaf
class DP1D,DP2D,DPBIT,GREEDY,HEAP,HEAPMERGE leaf
class SUBSETS,PERMS,COMBSUM,PLACEMENT leaf
class FIXEDWIN,VARWIN,SUBSEQ,NEXTGT,NESTING leaf
class COMPONENTS,DEPORDER,CYCLE,CLONE,MST,MAXFLOW leaf
class LRU,MINSTACK,SORTEDDS,GENERIC leaf
class SORTED,FINDGRAPH,OVERLAP,TOPK,CONTIG decision
Problem Type Quick Reference
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
ASI Domain Mapping
When an interviewer frames a problem in aviation terms, translate to fundamentals:
| Aviation Problem |
Algorithm |
Implementation |
| Route aircraft around weather |
A*, weighted graph |
graphs/a_star_search.py |
| Track nearby aircraft |
Geohash, KD-tree |
graphs/geohash_grid.py, graphs/kd_tree.py |
| Schedule maintenance with deps |
Topological sort |
graphs/topological_sort.py |
| Optimize fuel across routes |
Dijkstra, Bellman-Ford |
graphs/dijkstra.py, graphs/bellman_ford.py |
| Real-time position streaming |
Sliding window |
sliding_window/ |
| Airspace network planning |
MST, network flow |
graphs/minimum_spanning_tree.py, graphs/network_flow.py |
| Signal processing (ADS-B, radar) |
FFT, DCT |
concepts/fft_dct.py |
| API validation |
Pydantic, Flask |
concepts/validation.py, concepts/modern_flask.py |