How to solve a coding interview problem, out loud
A repeatable six-step method for coding interviews: understand, examples, brute force, optimize, code, test. What to say in each phase and how to split 45 minutes.
In short
Interviewers grade how you think as much as the final code. Follow the same steps every time: understand → examples → brute force → optimize → code → test, and say each one out loud.
The 45 minutes, phase by phase
Click each phase to see its goal and what it sounds like on a classic problem: find two numbers in an array that add up to a target.
01 · Goal
Understand
Restate the problem and pin down inputs, outputs and edge cases.
What it sounds like (Two Sum)
"So I get an array of integers and a target, and return the indices of two numbers that add up to it. Can there be duplicates? Is there always exactly one answer?"
"What size is the input? Negative numbers?"
The mistakes that sink good candidates
- Coding immediately. Five minutes of questions save twenty minutes of rewriting.
- Silence. If you think quietly, the interviewer can't give you credit or a hint.
- Skipping the brute force. Saying it first shows you can always deliver something correct, and gives you a baseline to improve.
- Not testing. Trace one example through your code before saying "done"; find your own bug before they do.
Optimizing = recognising a pattern
Most optimizations remove repeated work using one of a handful of patterns. Learning to spot them is what this playbook is about:
| If you see… | Try… |
|---|---|
| "Have I seen this before?" in a loop | A hash map or set |
| A sorted array, pairs or in-place edits | Two pointers |
| A contiguous subarray or substring | Sliding window or prefix sums |
| Sorted data, or "minimum value such that…" | Binary search |
| Trees, graphs, "all combinations" | DFS / BFS / backtracking |
| Overlapping subproblems | Dynamic programming |
Key takeaways
- 01Use the same six steps every time and narrate them.
- 02Ask questions and work examples before writing code.
- 03State the brute force and its complexity, then optimize.
- 04Test by tracing an example, and state the final time and space.
Check your understanding
1 of 3
What should you do first when you get a problem?
Related topics
Big-O
Big-O answers "if the input doubles, what happens to the work?" It ignores constants and keeps the fastest-growing term: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ).
8 min read →Hash maps
A hash map stores key → value pairs and finds any key in O(1) on average. Whenever a loop keeps asking "have I seen this before?", a hash map usually turns O(n²) into O(n).
8 min read →