Skip to content

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.

15 min read0/6 questions

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 promptPattern to tryWhy it fits
"contiguous subarray", "substring", "consecutive days"Sliding window or prefix sumsThe answer is a range [l, r], so you can grow and shrink one range instead of restarting.
"longest / shortest substring such that..."Variable sliding windowGrow right until the rule breaks, shrink left until it holds.
"sum of a subarray equals k" (negatives allowed)Prefix sums + hash mapsum(l..r) = prefix[r] - prefix[l-1], so look up prefix - k.
"sorted array", "sorted in ascending order"Two pointers or binary searchOrder 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 pointersToo small: move left up. Too big: move right down.
"in place", "O(1) extra space"Two pointers, overwrite the inputYou 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 countYou need counts per value, with O(1) updates.
"without extra space" and "every element appears twice except one"XORx ^ x = 0 and x ^ 0 = x, so pairs cancel and the single one survives.

Stacks, heaps, and intervals

Phrase in the promptPattern to tryWhy it fits
"valid parentheses", "nested", "matching"StackThe last thing opened must be the first closed: last in, first out.
"next greater", "next warmer day", "previous smaller"Monotonic stackKeep 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 kYou 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 heapsOne heap for each half gives O(log n) inserts and O(1) median.
"intervals", "meetings", "overlapping ranges", "merge"Sort by start, sweepAfter sorting, any overlap is with the previous interval only.
"minimum number of rooms / platforms at once"Sort events or min-heap of end timesThe answer is the peak of active intervals.

Recursion, graphs, and DP

Phrase in the promptPattern to tryWhy it fits
"return all combinations / subsets / permutations"BacktrackingYou 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)BFSBFS visits nodes in order of distance, so the first time you reach the goal is the shortest.
"shortest path" with weightsDijkstra (heap)BFS levels no longer equal distance when edges cost different amounts.
"grid", "islands", "connected", "regions"DFS or BFS on a grid graphEach cell is a node with up to four neighbours.
"prerequisites", "order of tasks", "dependencies"Topological sortA directed graph; you need an order that respects every edge, or a cycle.
"number of ways", "minimum cost to reach", "can you reach"Dynamic programmingThe answer for n is built from answers for smaller n, and those overlap.
"maximum profit, but you cannot pick adjacent"DP with take / skipEach choice depends on the previous one or two.
"minimise the maximum", "smallest speed such that"Binary search on the answerIf speed s works, every faster speed works too: a yes/no boundary to search.
Match the pairs0/5 matched

Arrays and strings: match the phrase to the pattern.

Choose an item, then its match on the other side.

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.

'Next warmer day' means monotonic stack
73
0
↑i
74
1
75
2
71
3
69
4
72
5
76
6
stack = [0]

Day 0 (73) has no answer yet. Push its index.

1/5
Match the pairs0/5 matched

Stacks, heaps, intervals: match the phrase to the pattern.

Choose an item, then its match on the other side.
Match the pairs0/5 matched

Graphs, recursion, DP: match the phrase to the pattern.

Choose an item, then its match on the other side.

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.
Quick check
'Find the length of the longest subsequence in which each number is larger than the last.' Which pattern?
Need a nudge? A hint rules out a wrong answer.
Quick check
'Return the number of distinct ways to make amount 100 from coins [1, 5, 10, 25].' Why not backtracking?
Need a nudge? A hint rules out a wrong answer.
Quick check
'Minimum total time for a signal to reach every node; each edge has a travel time.' A learner says BFS. What is wrong?
Need a nudge? A hint rules out a wrong answer.

The Decoder has a clue stage for every practice problem: you underline the phrases yourself, then compare with the model breakdown.

ChallengeMedium

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^5
  • 30 <= temperatures[i] <= 100
def daily_temperatures(temperatures: list[int]) -> list[int]: # Keep a stack of days still waiting for a warmer day. pass
Key terms
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.