Lesson 29 of 30
Watch the six-step framework solve a real, classic interview question from scratch — brute force first, then the fast version, narrated the whole way.
14 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
This lesson is different — there's no new concept to learn. Instead, we're solving one real, famous interview question from scratch, using the framework and habits from the last few lessons, so you can see them work together on an actual problem.
The question: "Given an array of numbers and a target, find two numbers that add up to it." This is called Two Sum — one of the most commonly asked interview questions, not because it's hard, but because how you approach it reveals how you think.
Step 1 — restate it. "I need to find two numbers from the list that add up to exactly the target, and probably return their positions."
Step 2 — small example by hand. Say nums = [2, 7, 11, 15], target = 9. By hand: 2 + 7 = 9. Found it — index 0 and 1.
Step 3 — brute force. "The most obvious thing I can do is check every possible pair and see if any add up to the target."
Step 4 — speed it up. "For each number, I'm asking 'have I already seen target - thisNumber?' If I remember every number seen so far, in something with instant lookup, I only need one pass."
// BRUTE FORCE — check every pair. Correct, but checks pairs we
// don't need to: O(n^2) time.
function twoSumBruteForce(nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j]; // found the pair, return their positions
}
}
}
return null; // no pair found
}
// OPTIMIZED — a hash map remembers numbers we've already seen, so
// we can ask "have I seen my missing piece?" in one pass: O(n) time.
function twoSum(nums, target) {
const seenSoFar = new Map(); // number -> its index
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i]; // the number that would pair with this one
if (seenSoFar.has(complement)) {
return [seenSoFar.get(complement), i]; // found it — the pair is complete
}
seenSoFar.set(nums[i], i); // remember this number for later
}
return null;
}
// Step 6 — test against our example: twoSum([2, 7, 11, 15], 9) -> [0, 1]. Matches!
Every step above is straight out of the six-step framework: restate, small example, brute force, speed it up, code it, test it. The "speed it up" step is the hash-map pattern from earlier in this track, recognized here by its trigger phrase: "I need to check if I've seen something before, fast."