Lesson 4 of 27Read before you code
Clues in the Wording
Spot the trigger phrases that point to a pattern, know why each one works, and avoid the phrases that mislead.
Problem setters cannot hide the structure of a problem completely, because they have to describe it precisely. The precise words they pick ("contiguous", "sorted", "k-th largest", "minimum number of moves") are fingerprints of the intended pattern. Experienced solvers are not smarter; they have simply seen these phrases enough times to recognise them in a second.
This lesson is a phrase dictionary. For each trigger you get the pattern and the reason, because the reason is what lets you tell a real clue from a coincidence.
Arrays and strings
| Phrase in the prompt | Pattern to try | Why it fits |
|---|---|---|
| "contiguous subarray", "substring", "consecutive days" | Sliding window or prefix sums | The answer is a range [l, r], so you can grow and shrink one range instead of restarting. |
| "longest / shortest substring such that..." | Variable sliding window | Grow right until the rule breaks, shrink left until it holds. |
| "sum of a subarray equals k" (negatives allowed) | Prefix sums + hash map | sum(l..r) = prefix[r] - prefix[l-1], so look up prefix - k. |
| "sorted array", "sorted in ascending order" | Two pointers or binary search | Order lets you discard half the search, or move one end inward knowing which way the sum goes. |
| "pair / triplet that sums to target" (sorted) | Two pointers | Too small: move left up. Too big: move right down. |
| "in place", "O(1) extra space" | Two pointers, overwrite the input | You cannot build a new array, so a read pointer and a write pointer share one. |
| "anagram", "frequency", "appears more than" | Hash map or 26-slot count | You need counts per value, with O(1) updates. |
| "without extra space" and "every element appears twice except one" | XOR | x ^ x = 0 and x ^ 0 = x, so pairs cancel and the single one survives. |
Stacks, heaps, and intervals
| Phrase in the prompt | Pattern to try | Why it fits |
|---|---|---|
| "valid parentheses", "nested", "matching" | Stack | The last thing opened must be the first closed: last in, first out. |
| "next greater", "next warmer day", "previous smaller" | Monotonic stack | Keep the unanswered items in a stack; a big value answers all smaller ones on top. |
| "top k", "k-th largest", "k closest", "k most frequent" | Heap of size k | You only need the best k, so keep a heap of k and drop the rest: O(n log k). |
| "running median", "stream of numbers" | Two heaps | One heap for each half gives O(log n) inserts and O(1) median. |
| "intervals", "meetings", "overlapping ranges", "merge" | Sort by start, sweep | After sorting, any overlap is with the previous interval only. |
| "minimum number of rooms / platforms at once" | Sort events or min-heap of end times | The answer is the peak of active intervals. |
Recursion, graphs, and DP
| Phrase in the prompt | Pattern to try | Why it fits |
|---|---|---|
| "return all combinations / subsets / permutations" | Backtracking | You must list every option, so you build each one choice at a time and undo it. |
| "minimum number of steps / moves" (each move costs the same) | BFS | BFS visits nodes in order of distance, so the first time you reach the goal is the shortest. |
| "shortest path" with weights | Dijkstra (heap) | BFS levels no longer equal distance when edges cost different amounts. |
| "grid", "islands", "connected", "regions" | DFS or BFS on a grid graph | Each cell is a node with up to four neighbours. |
| "prerequisites", "order of tasks", "dependencies" | Topological sort | A directed graph; you need an order that respects every edge, or a cycle. |
| "number of ways", "minimum cost to reach", "can you reach" | Dynamic programming | The answer for n is built from answers for smaller n, and those overlap. |
| "maximum profit, but you cannot pick adjacent" | DP with take / skip | Each choice depends on the previous one or two. |
| "minimise the maximum", "smallest speed such that" | Binary search on the answer | If speed s works, every faster speed works too: a yes/no boundary to search. |
Arrays and strings: match the phrase to the pattern.
Watching a clue become an algorithm
"How many days until a warmer day" is a next greater element clue. Here is why a stack fits: days that have not found their warmer day yet wait on the stack, coolest on top. A warm day answers everyone cooler than it.
Day 0 (73) has no answer yet. Push its index.
Stacks, heaps, intervals: match the phrase to the pattern.
Graphs, recursion, DP: match the phrase to the pattern.
Phrases that mislead
Some words look like clues and are not. These are the traps interviewers enjoy.
- "Subsequence" is not "subarray". A subsequence can skip elements, so there is no window. Longest Increasing Subsequence and Longest Common Subsequence are DP.
- "Return the answer sorted" is not "the input is sorted". Sorting the output tells you nothing about how to search the input.
- "Pair that sums to target" on an unsorted array, returning indices. Two pointers needs sorting, and sorting destroys the indices. Use a hash map (Two Sum).
- "K-th smallest in a BST". It sounds like a heap, but an in-order walk of a BST visits values in sorted order. Stop at the k-th.
- "Minimum number of steps" on a weighted graph. BFS counts edges, not weights. With weights you need Dijkstra.
- "All possible ways" when the output is a number. If you only return the count, you do not need to list them. That is DP, not backtracking.
- "Maximum subarray sum". It contains "subarray" but a window does not work with negatives. Kadane's algorithm (a tiny DP) does.
[1, 5, 10, 25].' Why not backtracking?The Decoder has a clue stage for every practice problem: you underline the phrases yourself, then compare with the model breakdown.
Daily Temperatures
Given a list of daily temperatures, return a list answer where answer[i] is how many days you have to wait after day i for a strictly warmer day. If no warmer day comes, answer[i] is 0.
[73, 74, 75, 71, 69, 72, 76, 73] → [1, 1, 4, 2, 1, 1, 0, 0]
[30, 40, 50, 60] → [1, 1, 1, 0]
[60, 50, 40] → [0, 0, 0]Constraints
1 <= temperatures.length <= 10^530 <= temperatures[i] <= 100
- trigger phrase
- Wording that fingerprints a pattern: *contiguous* for windows, *k-th* for heaps, *minimum steps* for BFS.
- subarray vs subsequence
- A subarray is contiguous; a subsequence may skip elements. Windows work only for subarrays.
- next greater element
- For each index, the nearest later value that is bigger. Solved with a monotonic stack in O(n).
- count vs list
- *Number of ways* usually means DP; *return all* means backtracking.
- misleading phrase
- A word that resembles a clue but does not fit, like *pair* on unsorted input that needs indices.