Problem Solving Framework
The candidate who solves silently and the one who talks through a slightly worse solution usually get different outcomes: the interview simulates working with you. A framework stops you freezing.
The 60-second answer
Section titled “The 60-second answer”I run the same six steps every time. Clarify the input, output and constraints. Work a small example by hand. State a brute force and its complexity. Find the bottleneck and name a better pattern. Code it in small, testable pieces. Then trace a normal case, an edge case and an empty input before saying I’m done. I narrate the whole way, and when I get a hint I say what it changes.
The six steps
Section titled “The six steps”1. Clarify (2–4 minutes)
Section titled “1. Clarify (2–4 minutes)”Never start coding on an ambiguous prompt. Ask about:
- Input: type, size, range, sorted?, duplicates?, empty allowed?, unicode?
- Output: an index, a value, all solutions, a count? What if there is no answer?
- Constraints: how large can n be? That number sets the required complexity.
- Environment: may I mutate the input? Extra memory? Recursion depth?
The constraint number decides the target: n ≤ 20 allows 2ⁿ, n ≤ 500 allows O(n³), n ≤ 5,000 allows O(n²), and anything at 10⁶ or above needs O(n log n) or O(n).
2. Examples (2–3 minutes)
Section titled “2. Examples (2–3 minutes)”Write two or three inputs and the expected output: one normal, one edge (empty, single element, duplicates), one tricky (negatives, already sorted). Working it by hand catches misinterpretations before they cost you ten minutes of wrong code.
3. Brute force (2 minutes)
Section titled “3. Brute force (2 minutes)”Say the obvious solution out loud, with its complexity. It proves you can solve it, gives you a fallback, and identifies the bottleneck you are about to remove.
“The brute force checks every pair — O(n²). For n = 10⁵ that’s too slow, so I want O(n log n).”
4. Optimise (5–8 minutes)
Section titled “4. Optimise (5–8 minutes)”Ask the four questions that unlock most problems:
- What am I recomputing? Cache it → hash map, memoisation, prefix sums.
- What order would make this easy? Sort it → two pointers, binary search, greedy.
- What structure gives me the operation I keep doing? Min → heap; membership → set; prefix → trie.
- Can I decide instead of search? Binary search on the answer.
If you are stuck, walk the brute force on the example and look for wasted work.
5. Code (10–15 minutes)
Section titled “5. Code (10–15 minutes)”- Write the signature and a stub first, then fill in.
- Meaningful names;
i,jare fine for indices,xis not. - Handle base cases at the top, in one place.
- Get it correct first, then mention what you would clean up.
# Statement: return indices of the two numbers that add to target.# Clarified: exactly one solution, cannot use the same element twice, n can be 10^5.# Brute force: O(n^2). Better: one pass with a hash of seen values -> O(n).
def two_sum(nums, target) seen = {} # value => index nums.each_with_index do |n, i| complement = target - n return [seen[complement], i] if seen.key?(complement) seen[n] = i end nil # problem guarantees a solution; nil is defensiveend
two_sum([2, 7, 11, 15], 9) # => [0, 1]two_sum([3, 3], 6) # => [0, 1]6. Test (3–5 minutes)
Section titled “6. Test (3–5 minutes)”Trace, do not eyeball. Say the variable values as you go.
- The example from step 2, then empty input and single element.
- All duplicates, all equal.
- Maximum size — does it still fit the complexity budget?
- Off-by-one boundaries: first element, last element.
Only then say “I think this is correct”. If you find a bug, that is a good sign: you tested.
Communication while coding
Section titled “Communication while coding”- Narrate decisions, not keystrokes. “A hash here makes the lookup O(1)” beats “I type
seen = {}”. - Announce before you change direction. “Actually a heap is better here because…”
- Say when you are unsure. “I believe this is O(n log n); let me verify by counting iterations.”
- Do not go silent for long. “Give me 30 seconds to work through the recurrence.”
Trade-offs to name out loud
Section titled “Trade-offs to name out loud”| Choice | You gain | You pay |
|---|---|---|
| Hash map | O(1) lookup | O(n) memory, no order |
| Sort first | Enables two pointers | O(n log n) upfront |
| Memoisation | Simple code | Stack depth, cache memory |
What gets people rejected
Section titled “What gets people rejected”- Coding before clarifying, then solving the wrong problem.
- Silence: the interviewer cannot score reasoning they cannot hear.
- Claiming a complexity you cannot justify when asked “why?”.
- Not testing. Failing the empty input reads as careless.
Follow-up questions
Section titled “Follow-up questions”- What would you change if the input did not fit in memory?
- How would you handle a stream rather than an array?
- What if there are multiple valid answers — do you need all of them?
- How would you test this in production, not just in the interview?
- What is the actual bottleneck if n doubles?
Common traps
Section titled “Common traps”- Skipping the brute force. You lose the fallback and the bottleneck analysis.
- Optimising before correctness. A working O(n²) beats a broken O(n).
- Ignoring the constraint number. n = 10⁵ means O(n²) is a fail, not a slow pass.
- Testing only the happy path. Edge cases are where interviews are decided.
- Treating a hint as an interruption. It is the interviewer steering you to the rubric.