Skip to lesson

Coding on a Whiteboard or Paper in Offline Interviews

At a campus drive or onsite round you may have to write Java on a whiteboard or paper, with no compiler to rescue you. The interviewer is checking that your code would compile and work, and that you can prove it by hand.

Step by step at the board

An offline interview is a round at a campus drive, walk-in or office where you write code by hand on a whiteboard or paper, without a compiler.

Never start by writing code. Follow the same six steps every time, so nerves have nothing to change. Talk while you work: silence at a whiteboard looks like being stuck, even when you are thinking well.

  1. Restate the problem and write the method signature at the top: static int maxProfit(int[] prices).
  2. Write two examples with their answers on the side, one normal and one edge case.
  3. Say the brute force and its complexity in one sentence, then the better approach.
  4. Get a nod from the interviewer: "Shall I code this approach?"
  5. Write the code top to bottom, saying each part as you write it.
  6. Dry run one example with a variable table, check edge cases, state the complexity.
Leave a blank line between groups of statements. You will almost always need to insert a line later, and boards have no "insert line" key.

Board layout, handwriting and naming

A helper method is a small named method for a repeated or long check, such as isOpen(c), that keeps the main logic short and easy to explain.

A tidy board reads like a tidy page. Example 1 is a board-ready bracket checker: a clear signature, two small helpers, meaningful names and five test calls.

Example 1 · A board-ready solution: signature, helpers, clear names

BalancedBracketsDemo.javaJava
import java.util.ArrayDeque;import java.util.Deque;
public class BalancedBracketsDemo {    // Signature first, top-left of the board    static boolean isBalanced(String s) {        Deque<Character> stack = new ArrayDeque<>();        for (char c : s.toCharArray()) {            if (isOpen(c)) {                stack.push(c);            } else if (stack.isEmpty() || !matches(stack.pop(), c)) {                return false;            }        }        return stack.isEmpty();    }
    // Small helpers written beside the main method    static boolean isOpen(char c) {        return c == '(' || c == '[' || c == '{';    }
    static boolean matches(char open, char close) {        return (open == '(' && close == ')')            || (open == '[' && close == ']')            || (open == '{' && close == '}');    }
    public static void main(String[] args) {        String[] tests = {"([]{})", "([)]", "((", "", ")("};        for (String t : tests) {            System.out.println("\"" + t + "\" -> " + isBalanced(t));        }    }}
Output
"([]{})" -> true
"([)]" -> false
"((" -> false
"" -> true
")(" -> false

What this code does

  • Two short helpers keep the main loop readable, and the names say what each piece means, which makes it easy to explain while writing.
  • The tests cover the normal case, wrong nesting, leftover opens, empty input and a close before any open.

Plan the board like a page: signature and code top-left, examples and edge cases in a narrow column on the right, and a free corner for the dry-run table. Write in straight, well-spaced lines, a little larger than feels normal, so the interviewer can read from a distance.

Use clear names (left, right, seen, windowSum) instead of a, b, x2, and check your board against this list:

  • Top-left: signature and code. Right column: examples, edge cases.
  • Bottom or a corner: dry-run table and complexity.
  • Helpers for repeated or long conditions, written beside the main method.
  • Consistent indentation, braces lined up, a blank line between groups of statements.

Writing Java without an IDE: the syntax that matters

Small slips are forgiven; wrong concepts are not. Example 2 is a one-page refresher of the calls that appear in most interview solutions, so you can write them without thinking.

Example 2 · One-page Java syntax refresher

SyntaxRefresherDemo.javaJava
import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Deque;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Queue;
public class SyntaxRefresherDemo {    public static void main(String[] args) {        int[] marks = {72, 95, 88};        String name = "Ravi";        List<Integer> list = new ArrayList<>(List.of(3, 1, 2));        System.out.println(marks.length + " " + name.length() + " " + list.size());
        Arrays.sort(marks);        Collections.sort(list);        System.out.println(Arrays.toString(marks) + " " + list);
        Map<Character, Integer> freq = new HashMap<>();        for (char c : "aab".toCharArray()) {            freq.put(c, freq.getOrDefault(c, 0) + 1);        }        System.out.println(freq.get('a') + " " + freq.getOrDefault('z', 0));
        Deque<Integer> stack = new ArrayDeque<>();        stack.push(1);        stack.push(2);        Queue<Integer> queue = new ArrayDeque<>();        queue.offer(1);        queue.offer(2);        System.out.println("stack " + stack.pop() + " " + stack.peek() + ", queue " + queue.poll() + " " + queue.peek());
        char c = 'c';        System.out.println((c - 'a') + " " + (char) ('a' + 2) + " " + Character.isDigit('7'));
        int[][] grid = new int[2][3];        StringBuilder sb = new StringBuilder("ab");        sb.append('c').reverse();        System.out.println(grid.length + "x" + grid[0].length + " " + sb + " " + Integer.MAX_VALUE);    }}
Output
3 4 3
[72, 88, 95] [1, 2, 3]
2 0
stack 2 1, queue 1 2
2 c true
2x3 cba 2147483647

What this code does

  • These are the calls that appear in most interview solutions: lengths, sorting, a frequency map, a stack and a queue on ArrayDeque, character arithmetic and 2D arrays.
  • Practise writing this file on paper from memory, then compile it; every mistake you find now is one you will not make at the board.

Interviewers forgive a missing import, a missing semicolon or size versus size() once you fix it. Learn these until they are automatic:

  • Length: arr.length (no brackets), str.length(), list.size().
  • Collections: new ArrayList<>(), new HashMap<>(), map.getOrDefault(k, 0), new ArrayDeque<>() with push and pop or offer and poll.
  • Characters: s.charAt(i), c - 'a', (char) ('a' + i), Character.isDigit(c).
  • Sorting and limits: Arrays.sort(arr), Collections.sort(list), Integer.MAX_VALUE, Math.max.

Interviewers do not forgive code that shows you do not know the language: a List<int>, a loop bound that cannot work, or methods that do not exist. Write the example from memory on paper, then compile it; each mistake you find now is one you will not make at the board.

Fix the error

Fix the slip

These slips came up on paper. Pick the replacement for the highlighted line.

Round 1 of 3

Count the marks in the array.

int[] marks = {56, 91, 78};
int n = marks.length();
System.out.println(n);

Error

error: cannot find symbol
  symbol:   method length()
  location: variable marks of type int[]

Dry running with a variable table, then edge cases aloud

A dry run is executing your code by hand on a small input, writing each variable's value per step in a table, to prove the code works.

A dry run proves the code without a compiler. Example 3 prints the table for moving zeroes to the end, one row per step, exactly as you would draw it on the board.

Example 3 · A dry run printed as a variable table

DryRunTableDemo.javaJava
import java.util.Arrays;
public class DryRunTableDemo {    // Move Zeroes: keep the order of non-zero values, push zeros to the end, in place.    static void moveZeros(int[] a, boolean trace) {        int write = 0;                              // next slot for a non-zero value        for (int read = 0; read < a.length; read++) {            if (a[read] != 0) {                int temp = a[write];                a[write] = a[read];                a[read] = temp;                write++;            }            if (trace) {                System.out.println("read=" + read + " write=" + write + "  " + Arrays.toString(a));            }        }    }
    public static void main(String[] args) {        int[] a = {0, 1, 0, 3, 12};        System.out.println("start              " + Arrays.toString(a));        moveZeros(a, true);
        int[][] edges = {{}, {0, 0}, {5}};        for (int[] e : edges) {            moveZeros(e, false);            System.out.println("edge case -> " + Arrays.toString(e));        }    }}
Output
start              [0, 1, 0, 3, 12]
read=0 write=0  [0, 1, 0, 3, 12]
read=1 write=1  [1, 0, 0, 3, 12]
read=2 write=1  [1, 0, 0, 3, 12]
read=3 write=2  [1, 3, 0, 0, 12]
read=4 write=3  [1, 3, 12, 0, 0]
edge case -> []
edge case -> [0, 0]
edge case -> [5]

What this code does

  • Each printed row is one row of the table you would draw on the board: loop variable, the other changing variable, and the array after that step.
  • The edge cases are then checked aloud: empty, all zeros and a single value all come out unchanged and correct.

Draw one column for the loop variable and one for every other variable that changes, add a row per iteration, and update the array as you go. Point at each line as you execute it.

Then walk through your edge cases aloud: "empty array: the loop does not run, I return 0, correct". If you find a bug, say so calmly: "this misses the last element; the bound should be <=". Fix it in place and rerun only the affected rows. Finding and fixing your own bug is a positive signal. Revise: Dry run on paper (opens in a new tab)

Predict the output

Fill the table in your head

Run each snippet by hand, row by row, then choose what it prints.

Round 1 of 2

What does it print?

int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i * i;
}
System.out.println(sum);

Shared editor habits: no autocomplete, compile mentally, test calls in main

To compile mentally is to check types, declarations, brackets and return paths by eye before you say you are done, because there is no compiler to do it.

With no compiler, you are the compiler. Example 4 is a solution with a check helper and five test calls, short enough to write from memory.

Example 4 · A template you can write from memory: solution plus test calls

Main.javaJava
import java.util.Objects;
public class Main {    // Max Consecutive Ones: longest run of 1s in an array of 0s and 1s.    static int longestRunOfOnes(int[] bits) {        int best = 0;        int current = 0;        for (int b : bits) {            current = (b == 1) ? current + 1 : 0;            best = Math.max(best, current);        }        return best;    }
    static void check(String name, Object expected, Object actual) {        String status = Objects.equals(expected, actual) ? "PASS" : "FAIL";        System.out.println(status + " " + name + ": expected " + expected + ", got " + actual);    }
    public static void main(String[] args) {        check("example", 3, longestRunOfOnes(new int[] {1, 1, 0, 1, 1, 1}));        check("all zeros", 0, longestRunOfOnes(new int[] {0, 0, 0}));        check("empty", 0, longestRunOfOnes(new int[] {}));        check("single one", 1, longestRunOfOnes(new int[] {1}));        check("run at the end", 2, longestRunOfOnes(new int[] {1, 0, 1, 1}));    }}
Output
PASS example: expected 3, got 3
PASS all zeros: expected 0, got 0
PASS empty: expected 0, got 0
PASS single one: expected 1, got 1
PASS run at the end: expected 2, got 2

What this code does

  • The check helper prints PASS or FAIL with both values, so a mistake is visible at once; on paper, writing the calls with expected values still shows you think in tests.
  • "run at the end" guards a common bug: updating best only when a 0 appears would miss a run that reaches the last element.

The same habits help when the interviewer gives you a plain editor without autocomplete. Before saying "done", check that every variable is declared with a type, every bracket is closed, return types match, and every path returns a value.

If time allows, write a short main with test calls, one per example and edge case, with the expected value next to each. On paper it shows that you think in tests; in an editor that runs code, it is how you prove the solution.

Find the bug

Compile it in your head

Each method would not compile. Click the line the compiler would complain about.

Largest mark.

Round 1 of 2

Next step

Check your understanding

12 quick questions