Facebook Pixel

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.

"Tree"

One question decides everything: do you care about depth, or just need to visit every node?

"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.

"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.

"Minimum/Shortest"

Three very different problems wear the same word.

"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.

"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

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro