How to explain your algorithm on the white board?

In many coding interviews, writing code is only half the task.
You also need to explain your thinking clearly and quickly.

This post gives you a simple structure you can use on a whiteboard, with a maxDepth example.

Why on white board?

A typical interview flow is:

  1. Understand the problem.
  2. Explain the approach.
  3. Write code.
  4. Walk through an example.

Before you write code, interviewers often check whether you can describe the algorithm in words.
That tests both your understanding and your ability to communicate precisely.

Good explanations are:

  • Definition-first
  • Short and concrete
  • Easy to verify with an example

Basic approach

Use this 4-step template:

  1. What is the problem asking?
  2. What strategy will you use?
  3. What does your function return?
  4. How does it run on a small example?

When relevant, finish with complexity (Time and Space).

Example

We will explain maxDepth for this tree:

      1
     / \
    2   3
   /
  4

1. What is the problem asking?

We need the number of nodes on the longest path from the root to a leaf.
For this tree, the answer is 3.

2. What strategy will you use?

Use DFS with recursion.

At each node, compute the depth of the left subtree and the right subtree, then take the larger one.

3. What does the function return?

For each node, the function returns:

the maximum depth of the subtree rooted at that node

Base and recurrence:

  • If node is None, return 0
  • Otherwise, return max(leftDepth, rightDepth) + 1

4. How does it run on this example?

Evaluate bottom-up:

  • Node 4: both children are None -> depth 1
  • Node 2: left 1, right 0 -> depth 2
  • Node 3: both children are None -> depth 1
  • Node 1: left 2, right 1 -> depth 3

Final answer: 3

Complexity:

  • Time: O(N) (visit each node once)
  • Space: O(H) (recursion stack, H = tree height)

Conclusion

If you want a clean interview explanation, keep it to these four steps:

  1. Problem definition
  2. Strategy
  3. Return value and recurrence
  4. Example walkthrough

This structure is short, precise, and easy for interviewers to follow.

Advanced tips: from brute force to optimized

A strong interview pattern is:

  1. Start with a correct brute-force idea.
  2. Explain its bottleneck.
  3. Propose and justify an optimization.

For sequencing, the safest default is:

  • Finish the 4 steps once for the brute-force version (at least briefly), then optimize.

Why this works:

  • You prove correctness first.
  • You show structured thinking.
  • The interviewer can follow your tradeoffs clearly.

If the brute-force solution is very long, you can optimize earlier at step 2, but still state:

  • The brute-force idea in one sentence
  • Why it is too slow
  • What changes in the optimized approach