---
title: Keywords
description: 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.
icon: map
---

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

## "Connected component"

Union Find — near-O(1) union and find beat re-running BFS after each edge. Use BFS or DFS instead when the graph is built once and you only traverse a few times. Number of Connected Components

## "Schedule" / "prerequisites" / "build order" / "course"

Topological Sort (Kahn's BFS or DFS post-order) — directed dependencies must be linearized so every edge points forward. Detects cycles as a side effect. Course Schedule

## "Transitive relationship"

If A relates to B and B relates to C means A relates to C, the relation is a graph in disguise. Items become nodes, relations become edges, and "are these two related?" becomes "are they in the same component?"

- BFS, to answer a single query by traversing on the fly. Word Ladder
- BFS or Union Find, for many queries on a fixed relation set. Sentence Similarity
- BFS or Union Find, for numbers with divisional relationships. Evaluate Division

## "Interval"

Greedy after sorting — by start (merge) or by end (fit-in), then one pass. Interval Pattern
