A blank page and a plan
After this: Name the six steps, restate two sum and ask four questions about it, and work its smallest example by hand.
On this page · 6 sections · 3 checks
You sit down with an interviewer. They read out a problem you have never seen, and then they wait. Many people start typing at once, because silence feels bad. A few minutes later they find they solved a slightly different problem, or they are stuck with half a program and no plan.
This tutorial gives you a plan for that moment. It is six steps, and you will follow all six on one problem, from a blank page to code you have tested.
The six steps
- Restate the problem and ask questions. Say it back in your own words, and ask about anything the statement leaves open.
- Work a tiny example by hand. Two to five items, on paper, so you know what the answer should be.
- Write the slow, obvious answer and count its cost. This is the : try every possibility.
- Find what makes it slow, and look for the cue. A is a phrase in the problem that points to a better tool.
- Write the code and walk through it. Then say what it costs.
- Test the edges. An is an input at the limit, such as an empty list.
Lessons 1 to 5 take these steps in order. This lesson does steps 1 and 2.
The problem
This tutorial writes each answer as two positions in square brackets, such as [1, 2]. In Python the input and the answer are both lists, and the empty answer is [].In Java the list is an int[] array and the answer is an int[] too. Arrays.toString prints it as [1, 2], and the empty answer, new int[0], as [].
Step 1: restate it and ask
Say it back: "I get a list of numbers and a target. I need two different positions whose numbers add up to the target, and I return those two positions." Then ask about what the statement leaves open. Here are four good questions, with the answers this interviewer gives:
| Question | Answer |
|---|---|
| Can I use the same item twice? | No. Two different positions. |
| Can there be no answer? | Yes. Then return an empty list. |
| Do you want the positions or the numbers? | The positions. |
| Can numbers be negative, or repeated? | Yes, both. |
Each answer changes the code you will write. The last one also gives you two edge cases to test in lesson 5.
Step 2: a tiny example by hand
Take the list [3, 2, 4] and the target 6. Look at the numbers one at a time:
- Position 0 holds 3. To make 6 it needs another 3, and there is no other 3.
- Position 1 holds 2. It needs a 4.
- Position 2 holds 4. That is the 4 that 2 needed.
So the answer is [1, 2], because 2 + 4 = 6.
Now look at the trap. 3 + 3 is also 6. But there is only one 3, at position 0, so [0, 0] would use the same item twice. The question "Can I use the same item twice?" ruled that out. Keep this trap in mind: lesson 3 meets it again inside the code.
The task below lets you look at the three numbers one position at a time, as you would on paper.
- Read index 0Read the number at position 0. It is 3. Could another number make 6 with it? Only another 3, and there is no other 3 in the list.
- Read index 1
- Read index 2
- 03
- 12
- 24
Run an operation to begin.
The rule: before you write any code, know what it should return on one small input. Then you can tell at once whether your code is right.
Check yourself
The interviewer finishes reading a problem you have never seen. What does the method say to do first?
Which restatement of two sum is right?
Work it by hand: nums is [5, 1, 5] and the target is 10. What should the function return?
Honesty notes
- Name the six steps: restate and ask, work a tiny example by hand, write the brute force and count it, find the cue, write and walk through the code, test the edges.
- Restate two sum in your own words: two different positions whose numbers add up to the target, or an empty list when no pair does.
- Ask the questions that settle the problem: the same item twice, no answer, positions or values, negative or repeated numbers.
- Work [3, 2, 4] with target 6 by hand to get [1, 2], and say why [0, 0] is wrong.