Lesson 11 of 27From idea to plan
The Pattern Map
Walk any problem from input type, through wording and constraints, to a short list of patterns worth trying.
You now have four separate signals: the shape of the input, the phrases in the wording, the constraints, and the shape of the output. On their own each one narrows things a little. Together they usually leave one or two candidate patterns. This lesson puts them on one map, so that when you meet a new problem you follow a route instead of guessing. It is the pattern cheat sheet from How to Solve Problems, turned into a route.
The map is a starting point, not a law. Its job is to give you a first pattern to try in under a minute, and a backup if the first one breaks.
The map
Read it left to right: what you are given, what the wording says, what the constraints allow, and the pattern that results. The last column is the fallback when a constraint or detail rules out the first choice. Press play to follow three real problems through it.
Longest Substring Without Repeating Characters: a string, *substring* means contiguous, n up to 5 · 10^4 and the rule only gets worse as the window grows. Sliding window.
The same map as a table
The diagram shows the five most common routes. Here are more, grouped by what you are given.
| Given | Wording / goal | Constraint or detail | Try first | Fallback |
|---|---|---|---|---|
| Array / string | contiguous, longest / shortest range | values non-negative | Sliding window | Prefix sums + hash map |
| Array / string | seen before, frequency, anagram | any | Hash map / set | Sort, or a 26-slot array |
| Array | next greater / smaller | n up to 10^5 | Monotonic stack | |
| Sorted array | find value, first / last position | O(log n) | Binary search | |
| Sorted array | pair / triple with a sum | O(1) space | Two pointers | Hash map if unsorted |
| Anything monotone | smallest x such that it works | answer range up to 10^9 | Binary search on the answer | |
| Grid / edges | islands, regions, connected | any | DFS or BFS | Union-find |
| Grid / edges | fewest steps | equal costs | BFS | Dijkstra if weighted |
| Edges with direction | order, prerequisites | any | Topological sort | DFS cycle check |
| Tree | height, path, subtree property | any | DFS recursion | Iterative DFS if very deep |
| Tree | per level, left / right view | any | BFS by level | |
| Items | all subsets / orders / placements | n ≤ 20 | Backtracking | |
| Items or a number | count ways, min cost, can reach | n up to 10^4 | Dynamic programming | Greedy if a local rule is provably safe |
| Values | top k, k-th, running median | k much smaller than n | Heap | Quickselect, sorting |
| Intervals | merge, overlap, rooms | any | Sort by start, sweep | Min-heap of end times |
| Numbers | appears once, without extra space | O(1) space | XOR, bit tricks |
Drill each region of the map
Arrays and strings: match the problem to its first pattern.
Sorted data and searching: match the problem to its first pattern.
Grids, graphs, and trees: match the problem to its first pattern.
Choices and counting: match the problem to its first pattern.
Heaps, intervals, and bits: match the problem to its first pattern.
When the map gives two answers
Often a problem matches two routes. "Pair that sums to target" is two pointers if sorted and a hash map if not. "Longest increasing" is DP if it is a subsequence and a window if it is a subarray. When that happens, go back to the detail that separates them and check it in the prompt, or ask the interviewer.
target.' The map gives sliding window and prefix sums. Which detail decides?Put the route through the map in order.
- Tap the steps below in the right order.
Rotting Oranges
A crate is a grid where 0 is an empty slot, 1 is a fresh orange and 2 is a rotten one. Every minute, each rotten orange rots the fresh oranges directly above, below, left and right of it. Return the number of minutes until no fresh orange is left, or -1 if some fresh orange can never be reached.
[[2, 1, 1],
[1, 1, 0],
[0, 1, 1]] → 4
[[2, 1, 1],
[0, 1, 1],
[1, 0, 1]] → -1 (bottom-left orange is cut off)
[[0, 2]] → 0 (nothing fresh to begin with)Constraints
1 <= grid.length, grid[i].length <= 10grid[i][j]is0,1or2
- pattern map
- A route from input shape, through wording and constraints, to a first pattern and a fallback.
- fallback pattern
- The second choice when a detail (negatives, weights, count only) breaks the first one.
- binary search on the answer
- When 'can it be done with x?' is yes for every x past some boundary, binary search for that boundary.
- multi-source BFS
- BFS that starts from many cells at once, so levels measure the time for all of them to spread.
- topological sort
- An order of a directed graph's nodes where every edge points forward. Exists only if there is no cycle.