Coding Interview Strategy
A reliable process matters more than memorised solutions.
The 7-step framework
- Clarify: restate the problem; ask about input size, ranges, duplicates, empty input, and expected output.
- Examples: work through a small example and an edge case by hand.
- Brute force: state the simplest solution and its complexity out loud.
- Optimise: look for the bottleneck; apply a pattern or data structure.
- Plan: describe the approach before writing code; get agreement.
- Code: write clean code with meaningful names; talk while you type.
- Test: trace an example, then edge cases; state final time and space complexity.
Pattern recognition
| Signal in the problem | Try |
|---|---|
| Sorted array, find pair or target | Two pointers, binary search |
| Contiguous subarray or substring | Sliding window, prefix sums |
| "Top K", "kth largest" | Heap |
| Need fast lookup or counting | Hash map or set |
| Next greater / matching brackets | Stack (monotonic stack) |
| Shortest path in unweighted graph or grid | BFS |
| All combinations, permutations, subsets | Backtracking |
| Overlapping subproblems, optimal substructure | Dynamic programming |
| Tree traversals | DFS recursion, BFS with a queue |
Review the basics: Arrays, Hash tables, Recursion, Searching and sorting.
When you are stuck
- Say what you have tried and what is blocking you; ask a targeted question.
- Simplify: solve a smaller version or a special case first.
- Draw it. Many problems become clear on paper.
- Never stay silent for more than a minute.
Communication template
"My understanding is ... Is that right?"
"A brute force approach is ..., which is O(n^2). We can do better by ..."
"I will use a hash map to ..., giving O(n) time and O(n) space."
"Let me trace this with the example ..."
Common mistakes
- Jumping into code before agreeing on an approach.
- Ignoring edge cases (empty input, single element, duplicates, negatives).
- Not testing the solution.
- Over-engineering: write the simplest correct solution first.
Loading Comments...