Say what you are doing while you do it
After this: Talk an interviewer through two sum, from restating the problem to the cost, and know what to say when you are stuck.
On this page · 6 sections · 3 checks
Picture two people solving the same problem in an interview. Both think for ten minutes and both write the right code. The first one works in silence. The second one says, every minute or so, what they are trying and why. The interviewer can only judge what they see and hear, so the second person shows far more of their skill than the first.
In an interview, the code is only part of your answer. The rest is what the interviewer hears while you write it and sees you check. This tutorial is about that part, in four habits: talk while you work, say the cost in words, try the edge cases, and run your code by hand before you say it is done.
Why talk at all
An interviewer wants to know how you think, not only whether you reach the answer. While you are silent, a good idea and a stuck mind look exactly the same from the outside. When you , you say in short sentences what you are doing and why. The interviewer can then follow you, correct a misreading early, and give a hint at the right moment.
You already know the six steps from Solving a problem you have not seen. Thinking out loud means saying one or two sentences for each of them.
A spoken script for two sum
The problem: given a list of whole numbers and a target, return the positions of two different items that add up to the target, or an empty list if no pair does. Here is what you might say, one line per step:
| Step | What you say |
|---|---|
| 1. Restate and ask | "So I need two different positions whose values add up to the target. Can I use the same item twice? Can there be no answer?" |
| 2. A tiny example | "With [3, 2, 4] and target 6, 2 plus 4 is 6, so the answer is [1, 2]. 3 plus 3 is also 6, but that uses position 0 twice." |
| 3. The slow answer | "The obvious way is to try every pair. That is O(n²) time: for 1,000 items, about half a million pairs." |
| 4. The cue | "The slow part is searching for the number I need. Looking up a value is what a hash map does, so I will keep a map from value to position." |
| 5. The code | "One pass. For each number I work out the one I need, look it up, and only then store this one." |
| 6. Cost and tests | "That is O(n) time and O(n) extra memory. Let me try [3, 3] with target 6, because duplicates are where this goes wrong." |
Step 3 names the : the slow, obvious answer that tries every possibility. Saying it first shows that you can solve the problem at all, before you make it fast. Step 4 names the : the words in the problem that point to a tool.
The costs in steps 3 and 6 are said in words too. O(n²) means the work grows with every pair of items, and O(n) means it grows with the size of the list. Lesson 2 gives the words for each cost.
While you type
When you write a line, say what it is for, not how it is spelled. The interviewer can read the code. What they cannot read is why the line is there.
- For the loop line, say "for each position and its value", not the words of the code.
- For need = target - xint need = target - x;, say "need is the number that would pair with this one".
- For the lookup, say "if the partner was seen earlier, I have my pair".
- For the store, say "only now do I remember this number, so it cannot pair with itself".
The trace below runs the code on [3, 2, 4] with target 6. Step through it and say each sentence as its line runs.
- Run the programRun it and step through slowly. At each line, say out loud what it is for, as you would to an interviewer. When the box seen changes, say what it now holds. When the condition is true, say which two positions are returned and why.
def two_sum(nums, target): seen = {} # value -> position for i, x in enumerate(nums): need = target - x if need in seen: return [seen[need], i] seen[x] = i return []
print(two_sum([3, 2, 4], 6))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
When you are stuck
Most people get stuck at some point in an interview. What matters is what the interviewer sees while you are stuck. Say three things: what you know, what you have tried, and what you are trying next. For example: "I have the slow answer working. The inner loop is the slow part, because it searches the list again for every item. I am looking for a way to remember what I have already seen."
That sentence gives the interviewer something to work with. If they want to help, they can say "what could remember it for you?", and you are moving again. Silence gives them nothing to hint at.
Check yourself
The interviewer reads out two sum and waits. Which first thing to say is best?
You are typing the line need = target - xint need = target - x;. Which sentence helps the interviewer most?
You have the slow answer working, and you cannot see a faster one yet. What is the best thing to do?
Honesty notes
- Say why thinking out loud matters: while you are silent, a good idea and being stuck look the same to the interviewer.
- Talk through two sum in six short lines, one per step: restate and ask, a tiny example, the slow answer, the cue, the code, the cost and tests.
- While typing, say what each line is for rather than reading its spelling aloud.
- When stuck, say what you know, what you have tried, and what you are trying next.