Loading…
Loading…
Charting your lesson…
Opening your lesson…
From Brute Force to Optimal — Thinking Out Loud
Lesson 2 of 4
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Interviewers can't read your mind — they grade the reasoning you say out loud. The strongest strategy is almost always: state a brute-force solution quickly, analyse why it's slow, then improve it step by step while narrating. This shows problem-solving in motion, and even if you never reach the optimal, a clearly-reasoned partial solution scores well.
What this covers / why it matters: the brute-force-first strategy, how to narrate your thinking, and how to systematically optimise. It's the core meta-skill of every coding round.
Step 2/ 61 min
Say the obvious solution first, even if it's O(n^2) or worse: "The brute-force way is to check every pair, which is O(n^2)." This gives you a working baseline, confirms you understand the problem, and gives the interviewer something concrete to react to. A brute force in hand beats an elegant solution you can't quite reach.
Ask "what is the wasteful part?" Usually it's repeated work. Common upgrades:
Name the bottleneck aloud, then propose the data structure or technique that removes it.
Think out loud about complexity as you go: "This drops it to O(n), but now I'm using O(n) extra space — that's fine given the constraints." Verbalising the time/space trade-off, and checking it against the input size you clarified earlier, is exactly what senior engineers do. If you get stuck, narrate that too — "I'm trying to avoid the second loop; let me think about what I'd need to remember as I scan once."
Step 3/ 61 min
Two-sum, narrated:
"Brute force: check every pair — O(n^2) time, O(1) space. That works but for n up to 10^5 it's 10^10 operations, too slow. The wasteful part is that for each element I re-scan the whole array looking for its complement. What if, as I scan once, I remember every number I've seen in a hash map? Then for each element x, I just check if target − x is already in the map — that's an O(1) lookup. So one pass: for each x, if (target − x) is in the map, return both indices; otherwise store x. That's O(n) time and O(n) space. Given extra space is allowed and n is 10^5, that's the right trade-off."
You moved from O(n^2) to O(n) while explaining every step — that narration is what's graded.
Step 4/ 61 min
Step 5/ 61 min
Freezing silently while trying to jump straight to the optimal solution. The interviewer sees a blank screen and no reasoning to reward. Always get brute force down and keep talking — a narrated, improving solution beats a silent perfect one every time.
Step 6/ 6· Worked examples1 min
Problem/Question: "Given an array and a target, return indices of two numbers that sum to the target (Two Sum)." — Walk from brute force to optimal, thinking aloud.
Solution/Model answer:
Step 1 — Brute force (state it first): "The obvious approach is to check every pair with nested loops. That's O(n^2) time, O(1) space. Let me confirm it's correct, then optimise." This shows you can always produce a working solution.
Step 2 — Identify the bottleneck: "For each element x, I'm re-scanning to find (target - x). If I could look that up instantly, I'd save the inner loop."
Step 3 — Optimise with a hash map: "I'll iterate once, and for each number check if (target - number) is already in a hash map of seen values; if yes, return both indices; otherwise store the current number. That's O(n) time, O(n) space."
Step 4 — Trade-off note: "I traded space for time — usually the right call unless memory is tight."
Answer/Takeaway: State a correct brute-force solution first with its complexity, identify the specific bottleneck, then optimise (often trading space for time via a hash map or better structure) — narrating your reasoning the whole way. Interviewers grade your problem-solving process, not just the final code.
Recall
Keep this
Interviewers score your problem-solving process, not just the final code. Stating a brute-force solution first, then improving it, shows structured thinking and guarantees you have something working even if you run out of time. Silence while you think reads as being stuck.
Narrate each step so the interviewer follows your reasoning and can nudge you.
Expected pattern: Every algorithmic round; interviewers explicitly say "walk me through your thinking."
Quick recap: Brute force (state complexity) → find bottleneck → apply a tool → new complexity → code. Narrate throughout.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.