Words in the problem point to a tool
After this: Say what a cue is, and see how one fact, a sorted list, changes the tool for a pair that adds up to a target.
On this page · 6 sections · 3 checks
In Solving a problem you have not seen, you found a faster answer to two sum by asking what made the slow answer slow. That works, but it takes time, and an interview gives you little of it. Many interview problems repeat a small number of shapes, and if you know the shapes, the words of the problem tell you where to start.
Words that point to a tool
Two sum asks for "two different items that add up to the target". For each number x, the partner you need is target − x, and "is that partner in the list, and where?" is a lookup by value. A answers that question in about one step, so a map from value to position solves two sum in one pass.
A phrase in a problem, or a fact about its input, that points to a tool is called a . This tutorial works through the common cues, each on a tiny problem.
One cue, two answers
A hash map would still work. But "sorted" is a second cue, and it points to a tool that needs no map: . Here a pointer is a variable that holds a position in the list, not a memory address. Put left at the first item and right at the last, and add the two values.
- If the sum is too big, only a smaller right value can help, so
rightmoves one place left. - If the sum is too small, only a bigger left value can help, so
leftmoves one place right. - If the sum is the target, you have the pair.
Work it on [1, 3, 4, 6, 8, 11] with target 10:
- 1 + 11 = 12. Too big:
rightmoves from index 5 to index 4. - 1 + 8 = 9. Too small:
leftmoves from index 0 to index 1. - 3 + 8 = 11. Too big:
rightmoves to index 3. - 3 + 6 = 9. Too small:
leftmoves to index 2. - 4 + 6 = 10. Found: the answer is
[2, 3], after 5 sums.
Step through it below; the narration says why each pointer moves.
- Two pointers: sum 10The row is sorted and the target is 10. Before each step, say which pointer you think will move, then read why it moved.
- 01
- 13
- 24
- 36
- 48
- 511
Run an operation to begin.
What the second cue saved
O(n) means the work grows with the length of the list, and O(1) means the same small amount whatever the length; Counting steps explains both.
| Problem says | Tool | Time | Extra memory |
|---|---|---|---|
| "add up to", list not sorted | hash map of value → position | O(n): one pass | O(n): the map |
| "add up to", list sorted | two pointers from both ends | O(n): at most n − 1 sums | O(1): two positions |
The sorted fact did not make the answer faster; it made the map unnecessary. So read every word of the statement: a small fact can change the tool. Two pointers from both ends explains why each move is safe.
The table of cues
Here is the whole table, with the tutorial that teaches each tool. The next four lessons work its rows, so there is no need to memorise it now.
| Cue in the problem | Tool | Taught in | Tiny problem → answer |
|---|---|---|---|
| "two items that add up to", not sorted | hash map of value → position | Hash Maps & Hashing | two sum, [3, 2, 4], target 6 → [1, 2] |
| "pair that adds up to", sorted | two pointers from both ends | Two pointers and sliding window | [1, 3, 4, 6, 8, 11], target 10 → [2, 3] |
| "seen before", "repeated", "unique" | hash set | Hash sets | first repeat in [4, 9, 2, 9, 4] → 9 |
| "how many of each", "most common" | hash map of counts | Hash Maps & Hashing | first character that appears once in "swiss" → 1 |
| "k in a row", "stretch", "substring", "subarray" | sliding window | Two pointers and sliding window | largest sum of 3 in a row in [2, 1, 5, 1, 3, 2] → 9 |
| "find in a sorted list", "first position where" | binary search | Sorted arrays and binary search | 23 in a sorted row of ten → index 5 |
| "matching brackets", "most recent first", "undo" | stack | Stacks and queues | "([])" → balanced; "([)]" → not |
| "nodes that point to the next", "reverse the list" | linked list with pointers | Linked lists | reverse the list 1, 2, 3, 4 → 4, 3, 2, 1 |
| "built from answers for smaller parts" | recursion | Recursion and the call stack | sum of [3, 1, 4] → 8 |
| "levels", "depth", "every node of a tree" | tree traversal | Binary trees and traversals | levels of a six-node tree → 3 |
| "k largest", "k smallest", "next most urgent" | heap | Heaps and priority queues | 2 largest of [5, 1, 9, 3, 7] → [9, 7] |
| "connected", "groups", "islands", "fewest steps" | BFS or DFS | Graphs: representation, BFS and DFS | groups in A–B–C, D–E, F → 3 |
| "cheapest", "shortest" with costs on the links | Dijkstra's algorithm | Weighted graphs: Dijkstra and topological order | A to E in the weighted graph → 7 |
| "before this can start", "prerequisites" | topological order | Weighted graphs: Dijkstra and topological order | the morning jobs → wake, coffee, … |
Check yourself
A problem says: “The prices are sorted from lowest to highest. Find two prices that add up to exactly 50.” Which tool do its words point to first?
Two sum on an unsorted list uses a hash map. The same problem on a sorted list uses two pointers. What does the word “sorted” change?
Two pointers on [1, 3, 4, 6, 8, 11] with target 10. The first sum is 1 + 11 = 12 and the second is 1 + 8 = 9. After the pointer moves that follow those two sums, where are left and right?
Honesty notes
- Say what a cue is: a phrase in a problem, or a fact about its input, that points to the tool that solves it well.
- Choose a hash map for a pair that adds up to a target in an unsorted list, and two pointers from both ends when the list is sorted.
- Move the pointers by the rule: a sum that is too big moves the right pointer in, a sum that is too small moves the left pointer in.
- Say what the sorted fact saved: the time stays O(n), and the extra memory drops from a map of n values to two positions.