Whiteboard Coding: Writing Clean Code
Fast recall
Quick revision
Core answer
Physical coding habits and whether the candidate communicates through code structure
Writing everything as one dense block with no spacing, making it unreadable
Before writing, outline your method signature and a rough structure comment, then fill in code — shows deliberate planning
Interview-ready guide
Interview answer
I treat the board as a shared draft that the interviewer and I must be able to trace. Before writing, I check the available space and place the method so there is room for a small example beside it. I use consistent indentation and braces, and I separate guard conditions from the main loop or recursion.
I keep familiar short names such as i for an index, but use descriptive names for domain values such as customerId or remainingAmount. I add a comment only when it explains a non-obvious invariant or branch. If I find a mistake, I mark the old line clearly and write the correction where both of us can see it.
The aim is not decorative handwriting. It is to make the algorithm, assumptions, and corrections easy to review together.
Whiteboard Coding: Writing Clean Code
Direct Answer
Use consistent indentation with spacing, write in a left-to-right top-to-bottom flow, use meaningful variable names even if short, and leave blank lines between logical sections. Plan your space before writing — start from the top-left.
Code Example
// Problem: Find if a user exists in a list by email
public boolean userExists(List<User> users, String email) {
// Guard clause for null inputs
if (users == null || email == null) {
return false;
}
// Linear search through user list
for (User user : users) {
if (user.getEmail().equalsIgnoreCase(email)) {
return true; // Found match
}
}
return false; // No match found
}
// Example usage:
// var users = List.of(new User("alice@example.com"));
// boolean exists = userExists(users, "alice@example.com");
Common Mistakes & Pitfalls
- No space plan — estimate the board area before writing so the method and example remain together.
- Ambiguous nesting — keep one indentation style throughout so loop and condition boundaries are visible.
- Names that hide meaning — short indices are fine, but domain values such as customerId should remain recognisable.
- Decorating instead of explaining — a few useful comments help; narrating every obvious line creates clutter.
Check your understanding
Answer these without looking back at the article.
- How do you handle running out of whiteboard space?
- How do you deal with syntax you are not 100% sure about during whiteboard coding?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.