Skip to content

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.

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.

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).

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.

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).”

Ask the four questions that unlock most problems:

  1. What am I recomputing? Cache it → hash map, memoisation, prefix sums.
  2. What order would make this easy? Sort it → two pointers, binary search, greedy.
  3. What structure gives me the operation I keep doing? Min → heap; membership → set; prefix → trie.
  4. 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.

  • Write the signature and a stub first, then fill in.
  • Meaningful names; i, j are fine for indices, x is 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 defensive
end
two_sum([2, 7, 11, 15], 9) # => [0, 1]
two_sum([3, 3], 6) # => [0, 1]

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.

  • 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.”
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
  • 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.
  1. What would you change if the input did not fit in memory?
  2. How would you handle a stream rather than an array?
  3. What if there are multiple valid answers — do you need all of them?
  4. How would you test this in production, not just in the interview?
  5. What is the actual bottleneck if n doubles?
  • 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.