"How Would You Test This?" How to Answer the Testing Follow-Up in a Coding Interview
You finish the solution, trace one example, and the interviewer nods. Then comes the question: "How would you test this?"
A lot of candidates treat this as a cool-down. They say "I'd try a few inputs, maybe an empty array," and wait for the next problem. That answer is not wrong, but it wastes a moment that interviewers use to separate people who write code that passes a demo from people who write code that holds up.
The good news is that a strong testing answer has a predictable shape. You do not need to invent it under pressure. You can practice it once, and then reuse it on almost every problem.
Why interviewers ask this
The testing question checks a few things at once:
- Do you know where your code is fragile? Someone who wrote the solution should know which lines are most likely to break.
- Can you think beyond the happy path? Real inputs are empty, huge, duplicated, negative, and malformed.
- Do you think like an engineer on a team? Tests are how code stays correct after other people change it.
- Can you organize your thinking? A structured answer reads as experience. A scattered list reads as guessing.
It also comes up in different forms. "What test cases would you write?" "How do you know this is correct?" "What would you check before shipping this?" All of them are the same question, and the same structure answers them.
The five-bucket answer
Instead of listing inputs as they come to mind, group them. Five buckets cover almost every coding problem, and naming the buckets out loud makes your answer sound deliberate.
1. The normal case
Start with one ordinary input that exercises the main logic, ideally the example from the prompt plus one you invent. Say what output you expect and why.
"First, a typical case: the example array, where I expect index pair (1, 3). Then one I make up with the answer at the very end, so the loop has to run fully."
This shows you are testing behavior, not just running the code.
2. Boundaries and edge cases
This is the bucket most people jump to, and it matters, but keep it specific to your code rather than reciting a generic list. Look at your loops, indexes, and conditions, and ask where they could go wrong.
Common ones:
- Empty input, single element, two elements.
- All elements identical, or all distinct.
- Values at the limits: zero, negatives, the maximum allowed size or value.
- The answer at the first or last position.
- No valid answer at all, if the problem allows that.
The strongest version ties each case to a line of code: "My window shrinks while left <= right, so I want a case where the window shrinks to zero length to make sure I don't skip the last element."
3. Invalid or unexpected input
Ask, or state your assumption, about inputs the problem did not promise: null values, wrong types, strings with unicode, unsorted data when you assumed sorted. You do not need to handle all of these in the interview, but you should mention which ones you would validate and which ones you would leave to the caller.
"I assumed the input is sorted. In production I'd either assert that or document it, and I'd add a test that confirms we fail loudly on unsorted input instead of returning a wrong answer."
4. Scale and performance
Correct code that times out is still a failure. Mention a test at the largest input size the constraints allow, and what you expect: "At 10^5 elements, my O(n log n) approach should finish well under a second. If I'd written the nested-loop version, this is the test that would catch it."
If memory is a concern, say so too, for example deep recursion on a long linked list that could overflow the call stack.
5. Confidence at volume: brute force comparison
This is the bucket that most candidates never mention, and it lands well with interviewers. If you have (or could quickly write) a slow but obviously correct version, you can generate many small random inputs and check that both versions agree.
"Since my optimized version is tricky, I'd keep the brute-force version as a reference, generate a few thousand random small arrays, and assert both return the same answer. That catches off-by-one errors that hand-picked cases miss."
This is sometimes called randomized or differential testing. You do not need the name. You need the idea, because it shows you know hand-picked tests have blind spots.
How long should the answer be?
Aim for two to three minutes. Name the buckets, give one or two concrete inputs for each, and stop. If the interviewer wants depth on one bucket, they will ask.
A compact version sounds like this:
"I'd group tests into five kinds. A normal case from the example. Boundaries: empty, single element, all duplicates, and the answer at the last index, since my loop condition is the riskiest line. Invalid input: unsorted data, which I'd assert against. Scale: the maximum n to confirm it stays O(n log n). And a randomized check against the brute-force version for small inputs, since that's how I'd catch subtle index bugs."
That is under a minute when spoken, and it covers far more ground than "I'd try an empty array."
Actually run one or two
If you are in a shared editor and time allows, offer to trace one of your edge cases right then. Pick the case most likely to break your code, not the one most likely to pass. If it fails, that is fine: you found the bug before the interviewer did, which is exactly what testing is for. Fix it calmly and say what you changed.
Candidates who dry-run their weakest case on purpose usually come across as more senior than candidates whose code happened to pass the easy one.
Mistakes to avoid
- Listing inputs with no expected output. A test without an expected result is just running code. Say what should come back.
- Only generic edge cases. "Empty, null, big" is a start, but tie at least one case to a specific line you wrote.
- Claiming it is fully correct. "I'm confident in the main path; the part I'd test hardest is the merge step" sounds more credible than "it handles everything."
- Turning it into a lecture. You do not need to discuss test frameworks, mocking, or CI unless asked. Stay on the code in front of you.
- Getting defensive if they find a case you missed. Treat it as a good catch, trace it, and adjust.
How to practice the testing answer
The testing follow-up is easy to skip in practice, because when you solve problems alone, nobody asks it. That is exactly why it feels awkward live.
A simple routine:
- After every practice problem, before checking the solution, write your five buckets in two or three lines each.
- Pick the riskiest case and trace it by hand.
- Once a week, for one problem, actually write a brute-force version and a small random comparison. Notice how often it finds something.
- Practice saying the compact version out loud, with a timer, until it takes under a minute.
After a few weeks, the structure becomes automatic, and the question that used to feel like an afterthought becomes one of the easier points in the round.
FAQ
Do interviewers expect me to write actual unit tests? Usually not in a short coding round. They expect you to describe test cases with expected outputs and, if time allows, trace one. Some companies with longer practical rounds do ask for real tests, so read the format notes from your recruiter.
What if I run out of time before the testing question? Mention testing briefly anyway as you finish: "If I had more time, I'd test the empty case and the all-duplicates case, since those hit my loop boundaries." It shows the habit even when the clock wins.
Is the brute-force comparison overkill for an interview? Describing it takes fifteen seconds and signals real engineering judgment. You rarely need to implement it live unless the interviewer asks.
Make the testing answer a habit
The testing follow-up rewards preparation more than talent. A five-bucket structure, practiced out loud a handful of times, turns a vague answer into a clear one.
If you want a calm place to rehearse that kind of structure beside your practice problems, Luna Interview is a Chrome side panel for interview practice that keeps prompts and notes next to your editor while you work through timed mocks: https://lunainterview.xyz/
Rehearse with an AI interview copilot, then browse commonly reported interview questions.