Skip to content

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

Scroll sideways if needed to explore the full diagram.

YES

Rotated?

NO

Unweighted

Weighted +

Weighted -

Heuristic

YES

NO

Kth element

Merge K

YES

NO

What does the problem ask for?

Find / search for something

Optimize a value
(min cost, max profit, count ways)

Generate all / enumerate

Process a sequence
(subarray, substring)

Graph structure
(explicit or implicit)

Design a data structure

Input is sorted?

Binary Search
searching/binary_search.py

search_rotated_array.py
find_minimum_rotated.py

Hash Map O(n) lookup
arrays/two_sum.py

Find in a graph?

BFS
graphs/number_of_islands.py

Dijkstra
graphs/dijkstra.py

Bellman-Ford
graphs/bellman_ford.py

A* Search
graphs/a_star_search.py

Overlapping
subproblems?

Dynamic Programming

1D: climbing_stairs, coin_change

2D: edit_distance, longest_common_subseq

Bitmask: traveling_salesman_dp

Greedy
merge_intervals, jump_game,
interval_scheduling

Top-K or
priority ordering?

Heap
heaps/kth_largest.py

Heap Merge
heaps/merge_k_sorted_lists.py

All subsets
backtracking/subsets.py

All permutations
backtracking/permutations.py

Combinations to sum
backtracking/combination_sum.py

Placement with rules
backtracking/n_queens.py

Contiguous?

Sliding Window

Fixed size: standard pattern

Variable: min_window_substring,
longest_substring_no_repeat

Subsequence DP
longest_increasing_subseq,
longest_common_subseq

Next greater/smaller?
Monotonic Stack
stacks_queues/daily_temperatures.py

Valid nesting?
Stack
stacks_queues/valid_parentheses.py

Connected components
DFS/BFS or Union-Find
graphs/number_of_islands.py

Dependency ordering
Topological Sort
graphs/topological_sort.py

Cycle detection
graphs/course_schedule.py

Clone / copy
graphs/clone_graph.py

Min spanning tree
graphs/minimum_spanning_tree.py

Maximum flow
graphs/network_flow.py

O(1) access + eviction
linked_lists/lru_cache.py

O(1) min/max retrieval
stacks_queues/min_stack.py

Sorted insert + search
bisect / SortedList

Generic typed container
concepts/advanced_typing.py


Problem Type Quick Reference

Arrays & Hashing

When You See...Use ThisImplementation
"Find two values that sum to X"Hash map lookuparrays/two_sum.py
"Group items by property"Hash map groupingarrays/group_anagrams.py
"Top K / most frequent"Bucket sort or heaparrays/top_k_frequent.py
"Product without self"Prefix/suffix arraysarrays/product_except_self.py

Two Pointers

When You See...Use ThisImplementation
"Three values sum to zero"Sort + two pointerstwo_pointers/three_sum.py
"Maximum area / container"Converging pointerstwo_pointers/container_with_most_water.py
"Water trapped between bars"Pointers + max trackingtwo_pointers/trapping_rain_water.py

Sliding Window

When You See...Use ThisImplementation
"Minimum window containing chars"Variable sliding windowsliding_window/min_window_substring.py
"Longest substring without repeat"Sliding window + hashsliding_window/longest_substring_no_repeat.py

Stacks & Queues

When You See...Use ThisImplementation
"Valid brackets/parentheses"Stack matchingstacks_queues/valid_parentheses.py
"Min/max in O(1)"Auxiliary stackstacks_queues/min_stack.py
"Next greater/warmer element"Monotonic stackstacks_queues/daily_temperatures.py

Graphs

When You See...Use ThisImplementation
"Connected components / islands"BFS/DFS flood fillgraphs/number_of_islands.py
"Dependency ordering"Topological sortgraphs/topological_sort.py
"Shortest path (positive)"Dijkstragraphs/dijkstra.py
"Shortest path (negative)"Bellman-Fordgraphs/bellman_ford.py
"Pathfinding with heuristic"A* searchgraphs/a_star_search.py
"Minimum cost to connect all"Kruskal's MSTgraphs/minimum_spanning_tree.py
"Maximum flow"Edmonds-Karpgraphs/network_flow.py
"Spatial proximity"Geohash / KD-treegraphs/geohash_grid.py, graphs/kd_tree.py

Dynamic Programming

When You See...Use ThisImplementation
"Count ways to reach target"Fibonacci-like DPdp/climbing_stairs.py
"Minimum cost to reach amount"Bottom-up DPdp/coin_change.py
"Longest increasing subsequence"DP + binary searchdp/longest_increasing_subseq.py
"String edit distance"2D DPdp/edit_distance.py
"0/1 Knapsack"1D optimized DPdp/knapsack.py
"Visit all cities"Bitmask DP (TSP)dp/traveling_salesman_dp.py

Backtracking

When You See...Use ThisImplementation
"Generate all subsets"Backtrackingbacktracking/subsets.py
"Generate all permutations"Backtrackingbacktracking/permutations.py
"Combinations summing to target"Backtracking w/ reusebacktracking/combination_sum.py
"N queens placement"Backtracking + pruningbacktracking/n_queens.py

Heaps & Greedy

When You See...Use ThisImplementation
"Kth largest element"Min-heap of size kheaps/kth_largest.py
"Merge k sorted lists"Heap mergeheaps/merge_k_sorted_lists.py
"Task scheduling"Max-heap + queueheaps/task_scheduler.py
"Merge overlapping intervals"Sort + mergegreedy/merge_intervals.py
"Can reach end? Min jumps?"Greedy max-reachgreedy/jump_game.py
"Max non-overlapping intervals"Greedy by end timegreedy/interval_scheduling.py

Pattern Recognition Keywords

When you read a problem statement, look for these keywords and jump to the right approach.

KeywordFirst Thing to TryFallback
"sorted"Binary search, two pointers--
"contiguous subarray"Sliding windowPrefix sums, Kadane's
"substring"Sliding window + hash mapDP
"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"BacktrackingBitmask enumeration
"merge" / "overlapping intervals"Sort + greedy sweep--
"kth largest" / "top k"Heap of size kQuickselect
"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 DPBFS (word ladder)
"knapsack" / "subset sum"DP (1D or 2D)--
"schedule tasks"Heap + greedyTopological 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

Editor-first interview practice. Content is tracked in git, and every packet page links to its source.

built from bb84dc1