Keyword to Algorithm
Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. "K-th largest" hints at a heap. "Shortest path" in an unweighted graph hints at BFS. Once you learn to read these signals, half the work is done before you write any code.
Each entry below names the keyword, the algorithm it points to, and a one-line reason. Where a keyword has more than one possible technique, each option is listed with the sub-signal that picks it. Techniques you haven't studied yet are covered later in the course.
"Top K"
Heap — track the K-item boundary in O(n log K) instead of sorting all n. K Closest Points
"Sorted" / "rotated sorted" / "in O(log n)"
Binary Search — halve the search space at each step using order. Works on any monotonic predicate, not just literal arrays. Binary Search
"How many ways..."
Counting distinct outcomes — choose by what the input looks like.
- DFS, when choices form a tree of decisions. Decode Ways
- DP, when subproblems overlap on a grid or sequence. Robot Paths
"Substring"
Sliding Window — contiguous slices extend and shrink in O(1) at each step. Longest Substring Without Repeating Characters
"Prefix" / "autocomplete" / "dictionary lookup" / "starts with"
Trie — share storage for common prefixes; lookup runs in O(length of word) regardless of dictionary size. Implement Trie
"Palindrome"
Palindromes mirror around a center — choose by whether you're verifying or constructing.
- Two Pointers, to verify a single string. Valid Palindrome
- DFS, to enumerate every valid partition. Palindrome Partitioning
- DP, to minimize cuts or count partitions. Palindrome Partitioning II
"Tree"
One question decides everything: do you care about depth, or just need to visit every node?
- BFS, for level-order, shallowest depth, or "right-side view". Binary Tree Level-Order Traversal
- DFS, for sums, heights, paths — anything not level-related. Max Depth
"Parentheses"
Stack — brackets close in reverse order, last opened first closed (LIFO). Valid Parentheses
"Next greater" / "next smaller" / "warmer day"
Monotonic Stack — keep candidates in stack order so the answer for each element pops out in O(1) amortized. Next Greater Element II
"Subarray"
Contiguous slices, but the right tool depends on what you're computing.
- Sliding Window, for fixed size or a monotonic constraint. Subarray Sum Fixed
- Prefix Sum, for range-sum queries. Subarray Sum
- Hashmap of prefix sums, for sum equals a target. Continuous Subarray Sum
"Max subarray"
Greedy (Kadane's) — at each index, extend the run or restart; the choice is local. Kadane's Algorithm
"Two sum / K sum"
Two Pointers on a sorted array — each move strictly raises or lowers the sum. Two Sum
"Max/longest sequence"
"Longest" usually means each position depends on earlier ones — except when a fixed window is involved.
- DP / DFS with memoization, when a position depends on earlier positions. Longest Increasing Subsequence
- Monotonic Deque, for max within a sliding window. Sliding Window Maximum
"Minimum/Shortest"
Three very different problems wear the same word.
- BFS, for fewest edges in an unweighted graph. Shortest Path
- Dijkstra, for shortest path with non-negative edge weights. Dijkstra's Algorithm
- DP / DFS, for cumulative cost on a grid or sequence. Minimal Path Sum
"Partition/split"
DFS — each split point is a recursive choice; memoize if suffixes repeat. Decode Ways
"Subsequence"
DP / DFS with memoization — pick-or-skip at each index, with overlapping subproblems. Longest Increasing Subsequence
"All combinations / permutations / subsets" / "generate every"
Backtracking — DFS that records a partial solution, recurses, then undoes the choice on the way back up. Permutations, Subsets
"Matrix"
A grid is a graph. Connectivity questions are BFS/DFS; optimization questions are DP.
- BFS / DFS, for connectivity, flood-fill, or region counting. Flood Fill, Number of Islands
- DP, for optimization where each cell builds on neighbors. Maximal Square
"Jump"
Greedy when local-optimal is provably global; DP otherwise.
- Greedy, when "always reach as far as possible" works (typically reachability). Jump Game
- DP, when counting jumps or greedy fails. Jump Game II
"Game"
DP on game states — "can the player to move force a win?" recurses with overlap. Divisor Game