Reasoning through a novel problem end to end
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Apply a repeatable method from restatement to tested solution.
- State brute force and its complexity before optimising.
- Name the trade-offs and edge cases an interviewer expects.
Overview
#A method you can repeat
When the problem is unfamiliar, fall back on a fixed sequence:
- Restate the problem in your own words and confirm the input and output types.
- Work two small examples by hand, including one edge case.
- Write the brute force and state its time and space complexity.
- Look for structure: sorted input, contiguous range, overlapping subproblems, graph shape.
- Choose a pattern from that structure and state the improved complexity.
- Implement, then trace one example against the code.
- List edge cases and say how the code handles them.
Worked example
Problem: find the length of the longest substring with no repeated characters.
- Restate: return the size of the largest contiguous window where all characters are distinct.
- Examples: 'abcabcbb' gives 3; 'bbbbb' gives 1; '' gives 0.
- Brute force: check every substring for uniqueness, O(n^2) or worse.
- Structure: a contiguous range with a validity condition is a sliding window.
- Pattern: expand right, and when a repeat enters, move left past the previous occurrence.
- Complexity: each index enters and leaves once, so O(n) time and O(k) space for the map.
- Edge cases: empty string, all identical characters, one character.
That is the whole conversation, from restatement to complexity to edge cases.
Trade-offs to name out loud
- Time versus space: caching speeds lookups but costs memory.
- Simplicity versus optimality: a clear O(n log n) solution may be the better answer under time pressure.
- Generality versus speed: a special-case solution can be faster but harder to maintain.
Communicating while you code
Narrate the plan, the invariant and the complexity. If you get stuck, say what you have ruled out and why. Interviewers weigh the reasoning as much as the final code.
Practising the method
Pick one hard problem, for example the minimum window substring or word ladder, and run the seven steps on paper before writing code. Repeat until the sequence is automatic. The goal is not memorised answers; it is a reliable process that turns an unfamiliar problem into a known pattern.
Practice next
- Run the seven-step method on the minimum window substring problem.
- Compare a BFS solution and a bidirectional BFS solution for word ladder.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Interview
You are asked to return the length of the longest substring without repeating characters. Walk through the first four steps of the method before writing any code.
Reveal the reference answer
reference.textRestate: the largest contiguous window with all distinct characters. Examples: 'abcabcbb' gives 3, 'bbbbb' gives 1, '' gives 0. Brute force: test every substring for uniqueness, O(n^2) or worse. Structure: a contiguous range with a validity condition suggests a sliding window, which should reach O(n).Authored expected output: Restatement, two examples plus an edge case, brute force with complexity, and the sliding-window structure.
Stating the brute force and its cost before optimising shows structured reasoning.
Exercise 2Interview
For the streaming median problem, explain which data structure you would choose and why, including the complexity of each operation.
Reveal the reference answer
reference.textUse two heaps: a max-heap for the lower half and a min-heap for the upper half. Keep their sizes within one of each other. Each insert is O(log n) because it touches one heap and possibly rebalances. The median is O(1): the top of the larger heap, or the average of both tops when the sizes are equal. This beats re-sorting, which would be O(n log n) per query.Authored expected output: Two heaps, O(log n) insert, O(1) median.
Naming the data structure and the per-operation cost is the expected depth for a hard problem.
Exercise 3Explain
Explain how you would decide between a BFS solution and a bidirectional BFS solution for word ladder.
Reveal the reference answer
reference.textPlain BFS from the start explores level by level and is correct but can visit many nodes. Bidirectional BFS expands from both the start and the target, meeting in the middle, which roughly halves the explored depth and can be much faster when the branching factor is large. I would start with plain BFS for clarity, then mention bidirectional BFS as an optimisation, and note that it needs careful handling of the meeting condition and visited sets.Authored expected output: Start with plain BFS; propose bidirectional BFS as a faster optimisation with added complexity.
Presenting a clear baseline and a justified optimisation is exactly the trade-off discussion interviewers look for.
Common misconceptions
#- You must jump straight to the optimal solution. A stated brute force and its complexity is often the expected first step.
- Edge cases are optional. They are frequently what separates a correct answer from a partial one.
- Naming complexity is showing off. It is how you justify that your approach is appropriate.
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.