Command Palette

Search for a command to run...

Lesson 44.1 · Interview Method and Simulation

The Seven-Step Method

Clarify → examples → brute force → optimise → plan → code → test. Spend the first third of the time before writing code.

16 min

Think of it like this

A pilot's checklist before take-off: it feels slow, but it's what stops experienced people making simple, expensive mistakes under pressure.

1.The steps

1. Clarify (2–3 min): input size and ranges, duplicates, negatives, empty input, sorted or not, what to return if no answer, whether you can modify the input.

2. Examples (2 min): work one normal and one edge example by hand. It confirms you understood the question.

3. Brute force (1–2 min): say the simplest correct idea and its complexity, even if it's slow. It shows you can always produce something.

4. Optimise (5–10 min): find the bottleneck ("the inner loop searches for a complement") and name the pattern that removes it ("a hash map makes the search O(1)"). State the new complexity.

5. Plan (1–2 min): outline the code in a few comments or bullet points before typing.

6. Code (10–15 min): clear names, small helper functions, no premature cleverness.

7. Test (3–5 min): walk through your own example line by line, then edge cases. Fix bugs you find out loud: finding your own bug is a positive signal.

Rendering diagram…

2.Testing your own code

In practice (and in some interviews) you can run code. A few assertions in a main method catch most mistakes quickly:

Main.java
import java.util.*;

public class Main {
    static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> seen = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer j = seen.get(target - nums[i]);
            if (j != null) return new int[]{j, i};
            seen.put(nums[i], i);
        }
        return new int[0];
    }

    static int passed = 0;
    static void check(int[] got, int[] want) {
        if (Arrays.equals(got, want)) passed++;
        else System.out.println("FAIL: got " + Arrays.toString(got) + ", want " + Arrays.toString(want));
    }

    public static void main(String[] args) {
        check(twoSum(new int[]{2, 7, 11, 15}, 9), new int[]{0, 1});     // normal case
        check(twoSum(new int[]{3, 3}, 6), new int[]{0, 1});              // duplicates
        check(twoSum(new int[]{1, 2}, 7), new int[0]);                   // no answer
        System.out.println(passed + " of 3 tests passed");
    }
}

Output

3 of 3 tests passed

Remember

  • Don't code before step 4.
  • Always state brute force + complexity.
  • Test by hand; fix out loud.

Common mistakes

  • Coding immediately and discovering a misunderstanding halfway.
  • Staying silent while thinking.