RoleGymAIinterview performance
All articles
Technical interviews

How to Test Code in a Coding Interview: A Spoken Edge-Case Routine

Test code in a coding interview without freezing: derive edge cases from the problem contract, dry-run one example, and explain what each test proves.

4 min readBy RoleGymAI

To test code in a coding interview, choose cases from the problem contract rather than reciting a generic list: one ordinary example, the smallest valid input, a boundary that changes the logic, and a case that challenges an assumption. Then dry-run one case aloud so the interviewer can see what your variables do.

Testing is part of showing your reasoning, not a ceremony after the code is finished. Microsoft's technical-interview guidance says candidates may be assessed on technical principles and problem-solving, and notes that engineers test their own and teammates' code. The useful interview habit is to connect each test to a risk in the solution.

Start from the contract

Before inventing edge cases, state what the function promises:

  • What is a valid input?
  • What output format is required?
  • Is order meaningful?
  • Can values repeat, be negative, or be empty?
  • Is the function allowed to modify its input?

An edge case only matters if it can occur under that contract or exposes an assumption you need the interviewer to confirm. For example, testing null makes sense only if the problem does not guarantee an array. Testing duplicate values matters if your approach uses a set or map and the output must preserve an order.

A four-case routine you can say aloud

After writing a first version, choose four purposeful checks:

  1. Representative case: confirms the main idea on an ordinary input.
  2. Smallest valid case: empty input, one item, or the minimum allowed size.
  3. Decision boundary: a value exactly at the threshold or the first input that takes another branch.
  4. Assumption challenge: duplicates, repeated calls, negative values, or a large input — whichever is relevant to your design.

This is a selection method, not a requirement to force four tests into every answer. If the input is guaranteed to have at least two sorted integers, an empty-array test may be irrelevant. Say that: “Because the prompt guarantees two values, I will focus on duplicate values and the first and last positions.”

Worked example: preserving first occurrences

Example — fictional prompt: “Given a list of integers, return the values in their original order with later duplicates removed.”

A reasonable solution keeps a set of values already seen and appends a value only the first time it appears:

def unique_in_order(values):
    seen = set()
    result = []
    for value in values:
        if value not in seen:
            seen.add(value)
            result.append(value)
    return result

Here is a concise test narration:

“I will first test [3, 1, 3, 2], which should return [3, 1, 2]; it checks both duplicate removal and original order. Next, [] should return [], because the loop simply has no work. Then [7] checks the smallest non-empty list. Finally, [4, 4, 4] checks that repeats do not create extra output. I will dry-run the first case: after the first 3, seen is {3} and result is [3]; when the second 3 arrives, it is skipped.”

The value of the explanation is not the particular numbers. It makes the invariant visible: seen contains every value already placed in result.

Add one test for a real design risk

The same code has a question worth raising: must the input list remain unchanged? This implementation leaves it alone. If you chose an in-place sort instead, say so and test the side effect separately. That prevents a correct-looking answer from violating an unstated expectation.

Turn common edge cases into questions

Do not memorise “empty, one, duplicates, negative, big” as magic words. Translate them into a question about your code.

| Pattern | Question to ask yourself | Example | |---|---|---| | Empty input | Does the initial value or loop behavior still make sense? | Does the total begin at 0? | | One item | Does an index or pointer assume a neighbor exists? | Does a two-pointer loop stop correctly? | | Repeated values | Does uniqueness, counting, or ordering change? | Does a set accidentally remove required repeats? | | Boundary | Which input is the first to take a different branch? | Is limit included or excluded? | | Extreme size | Does the approach stay within the likely time or memory budget? | Is an accidental nested loop avoidable? |

You are not expected to prove every possible failure in a short interview. Pick the two or three tests that most directly challenge the current approach.

What to do when a dry run exposes a bug

Say what changed your mind. For example: “On [3, 1, 3, 2], I see that I appended before checking seen, so the duplicate remains. I will move the append inside the condition and rerun this case plus the all-duplicate case.”

That is a strong answer because it connects observation, correction, and verification. For a fuller rehearsal of that sequence, use Debugging Interview Practice, which walks through an evidence-first approach to a failing example.

A two-minute finishing checklist

Before you say you are done, ask:

What does this function promise?
Which test shows the normal path?
Which smallest or boundary input could break my current branch logic?
What assumption does my data structure make?
Can I trace one example without skipping variable changes?

Practise this after every coding exercise. First write the cases, then explain one run without executing the program. That makes it easier to speak clearly when an interviewer asks how you know the code works.

When you are ready, save your likely coding prompts and project decisions in RoleGymAI Interview Preparation, then rehearse them aloud. The RoleGymAI blog also has a project-walkthrough exercise for explaining the code you have already built.