Say the cost in words
After this: Give the five costs in plain words with their step counts, and say the extra memory and the trade.
On this page · 6 sections · 3 checks
You finish two sum and say "it's O(n)". The interviewer nods and asks: "What does that mean for a list of a million numbers?" If O(n) is only a label you have learned, this question is hard. If you can say it in words, it is the easiest point of the interview.
In lesson 1 you said the cost at the end of the spoken script. This lesson gives you the words for each cost you have met, and the numbers that go with them.
The cost in words
The of a solution is how its number of steps grows as the input grows. You met O(1) and O(n) in Counting steps. Here are the five costs from the ten classic problems, each with a plain sentence and the step counts for a thousand items and for a million:
| Cost | In words | n = 1,000 | n = 1,000,000 | Example from the ten problems |
|---|---|---|---|---|
| O(1) | the same work for any size | 1 | 1 | a map lookup (on average) |
| O(log n) | one more step each time n doubles | about 10 | about 20 | binary search |
| O(n) | the work grows with the size | 1,000 | 1,000,000 | best day to buy and sell |
| O(n log n) | the usual cost of sorting | about 10,000 | about 20 million | sorting a list, such as all the counts in top k |
| O(n²) | every pair | 1,000,000 | 10¹² (a million million) | two sum by trying every pair |
Read the last two columns, not only the letters. At a thousand items every row looks manageable. At a million, the difference between 20 steps and a million million steps is the difference between an instant answer and a program that never finishes in an interview.
Count it on ten cells
The row below has ten sorted numbers. The task runs two searches for 91, the last number. The first looks at every cell from the start; the second is binary search. Count the steps of each.
- Search for 91Look at every cell from the start until 91 turns up. Count the comparisons: this is O(n), the work grows with the size of the row.
- Binary search for 91
- 02
- 15
- 28
- 312
- 416
- 523
- 638
- 756
- 872
- 991
Run an operation to begin.
- The scan compares 2, then 5, then 8, and so on, and finds 91 on the 10th comparison. Ten items, ten steps: O(n).
- Binary search probes 16, then 56, then 72, then 91. It finds it on the 4th probe, because each probe halves what is left: O(log n).
For a million items, the scan could need a million comparisons and binary search about 20. That is the sentence to say: "binary search needs about 20 steps for a million items, because each step halves what is left."
Say the memory too
Time is only one cost. The other is the : the space a solution needs besides its input. Say it without being asked.
- Two sum with a map keeps up to n entries: O(n) extra memory.
- The best day to buy and sell keeps two numbers, the lowest price and the best profit: O(1) extra memory.
- Reversing a linked list keeps three pointers, prev, cur and nxt: O(1) extra memory.
When one answer spends memory to save time, name the trade. For two sum: "the map costs O(n) memory, and in return the time drops from O(n²) to O(n)." An interviewer may well ask "can you do it without the extra memory?", and naming the trade first shows you already know what that question is about.
The rule
State both costs, each with a plain sentence: "O(n) time, because it goes through the list once; O(n) extra memory, for the map." Then give one number for a large input if it helps. Say "on average" when a hash map is involved, because its O(1) lookup is an average, not a promise.
Check yourself
Which cost do you mean when you say "one more step each time the input doubles"?
The number-of-islands solution from the ten classic problems goes over a grid and marks each land cell as seen once. How do you say its time cost in words?
Reversing a linked list with prev, cur and nxt turns every link around in one pass. What extra memory does it use?
Honesty notes
- Say the five costs in plain words: the same work for any size, one more step each time n doubles, grows with the size, the usual cost of sorting, every pair.
- Give the step counts for a thousand and a million items: about 10 and 20 for O(log n), a million and a million million for O(n²).
- Count the steps on ten sorted cells: 10 comparisons to scan for 91, 4 probes for binary search.
- State the extra memory as well as the time, and name the trade when an answer spends memory to save time.