Skip to the content

Lesson 117 of 296

The 7-step problem-solving method

What you will learn

  • A fixed method to use in every coding interview
  • What to say at each step
  • How to recognise which pattern a problem needs
  • What to do when you are stuck
On this page

Why a fixed method?

Interviewers mark your thinking, not just the final code. A candidate who talks through a clear process and reaches a good solution often scores higher than one who silently writes a perfect answer. A fixed method also stops panic: you always know the next step.

The 7 steps

1. Clarify (2 to 3 minutes)
Repeat the problem in your own words and ask questions:

  • What are the inputs and their types? Can they be empty? Negative? Duplicated?
  • What should I return? What if there is no answer?
  • How big can the input be? (This tells you the target complexity.)
  • Is the input sorted? Can I change it?

2. Examples (2 minutes)
Work through one normal example by hand. Then list edge cases: empty input, one item, all the same, very large values, negative numbers.

3. Brute force (2 minutes)
Describe the simplest correct solution and its Big-O. Do not necessarily code it. "The brute force checks every pair, which is O(n²)." This proves you can solve it and gives you a baseline.

4. Optimise (5 to 10 minutes)
Find the bottleneck: what work is repeated? Ask yourself:

  • Can a Map or Set remember what I have already seen?
  • Would sorting help?
  • Is the input sorted, so two pointers or binary search apply?
  • Am I looking at a range or subarray? Sliding window or prefix sums.
  • Is it a tree or graph? DFS or BFS.
  • Do I need all combinations? Backtracking.
  • Are the same subproblems solved again and again? Dynamic programming.

State the new complexity and get agreement before coding: "Shall I code this approach?"

5. Code (10 to 15 minutes)

  • Talk while you type.
  • Use clear names (left, right, seen), not a, b, x.
  • Write small helper functions.
  • If you forget a built-in method, say so and write a simple version.

6. Test (3 to 5 minutes)
Do not say "done" yet. Trace your code by hand with your example, line by line, tracking the variables. Then check the edge cases. Fix bugs calmly, out loud.

7. Analyse (1 minute)
State the final time and space complexity, and mention trade-offs or further improvements.

Recognising patterns

Clue in the problemLikely patternLessons
"Find a pair / count occurrences / seen before"Hash map or set122
"Sorted array", "pair that sums to"Two pointers or binary search123, 131
"Contiguous subarray / substring", "longest / shortest with condition"Sliding window125, 126
"Sum of a range", "subarray sum equals k"Prefix sums127
"Matching brackets", "undo", "next greater"Stack or monotonic stack128, 130
"Minimum value that works"Binary search on the answer132
"Linked list cycle / middle"Fast and slow pointers124
"All subsets / permutations / combinations"Backtracking135
"Tree", "levels"DFS or BFS136, 137
"Top k", "kth largest", "merge sorted lists"Heap139, 140
"Grid of islands", "shortest path in steps", "dependencies"Graph BFS/DFS, topological sort141, 142
"Shortest path with weights"Dijkstra143
"Overlapping ranges"Sort by start, then merge144
"Number of ways", "minimum cost", "can you reach"Dynamic programming146, 147
"Prefix of words", "autocomplete"Trie148

When you are stuck

  • Say what you are trying: "I am thinking about whether sorting helps here."
  • Go back to a small example and solve it by hand. Notice what you do; that is often the algorithm.
  • Try the patterns in the table one by one.
  • Ask for a hint. It is much better than silence. Interviewers expect to give hints.

Example: talking through Two Sum

"So I get an array of numbers and a target, and I return the indexes of two numbers that add up to the target. Can there be no answer? Is there always exactly one? Can I use the same element twice? ... The brute force checks every pair, O(n²) time, O(1) space. The repeated work is searching for the partner each time. If I store each number's index in a Map as I go, I can check for target - current in O(1), giving O(n) time and O(n) space. Shall I code that?"

How to say it in an interview

"I follow the same steps every time: clarify inputs, outputs, edge cases and constraints; walk through examples; state a brute force with its complexity; look for repeated work and match the problem to a pattern; agree the approach; code it while talking; trace it by hand with normal and edge cases; then state the final time and space complexity."

Key words

WordMeaning
Brute forceThe simplest solution that tries everything
Edge caseAn unusual input, such as empty or very large
BottleneckThe slowest part of a solution
PatternA known approach that fits a family of problems
TraceRun the code by hand, tracking each variable

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.