TDD Interview Questions: Answer With a Worked Example

What TDD interview questions really test, a full red-green-refactor cycle for sum_positive traced on real input, the words to say at each step, and honest answers when you have not done TDD on a job.

By AI Interview AssistantPublished 9 min read

A software developer at a desk with a laptop showing a code editor and a passing test, notebook and coffee nearby

A code editor split into two panes beside a notebook of handwritten test steps

TDD interview questions test whether you can drive a small piece of code from a failing test and explain each decision as you go. Expect to write a test first, watch it fail for the right reason, make it pass with the simplest change, then refactor and say what you would test next. Prepare one worked example you can write and narrate in about ten minutes, plus honest answers on when test-first is and is not worth it.

What a TDD interview actually asks you to do

Three formats come up. You may be handed a function signature and asked to write a test first. You may get existing code and a feature to add test-first. Or you may simply be asked to discuss a testing decision you made in real work.

All three check the same things: whether your test fails for the right reason before you write code, whether the implementation is the smallest change that passes, and whether you refactor with the test green and can name what you are doing. The interviewer watches the order of your edits. Reciting "red, green, refactor" while writing the implementation first scores badly, because the vocabulary is not the skill.

Say one thing early and then do it: "I'll write the test first and run it to see it fail." If you skip the run, you have told the interviewer you know the step and then shown that you do not use it. If you freeze later, fall back to the smallest possible test: one input, one expected output, run it.

A full red-green-refactor cycle you can write in ten minutes

The task: a function sum_positive(numbers) that returns the sum of the positive integers in a list and ignores zero and negatives.

Red

Write the test and run it before any implementation exists:

def test_sum_positive_ignores_negatives():
    assert sum_positive([3, -1, 2]) == 5

Run it and read the failure out loud: "NameError, the function does not exist yet." That is the right reason. A test that passes before the code exists is testing nothing.

Green

Write the smallest implementation that makes that test pass:

def sum_positive(numbers):
    total = 0
    for n in numbers:
        if n > 0:
            total += n
    return total

Trace it on [3, -1, 2]: total starts at 0, 3 is positive so total is 3, -1 is skipped, 2 is positive so total is 5. The test passes. Resist adding type checks, logging or a None guard, because no test asks for them yet. The loop runs once per element, so time is O(n) and the extra space is O(1): one accumulator, no copy of the list.

Refactor

With the test green you can rename safely. Change total to running_sum if that reads better, rerun the test, and leave the extraction of a helper for when a second test needs it. Then state the next test you would add: an empty list should return 0. That test will pass already, which is fine and worth saying, because it locks in the behaviour before the next change.

The words to say at each step

A script you can adapt:

Red: "This test should fail because the function doesn't exist. If it passes, my test is wrong."

Green: "Simplest thing that passes. I'm not handling None yet because no test asks for it."

Refactor: "The test is green, so I can rename this variable safely. I'll rerun before moving on."

The trap is writing the full implementation and then a test that happens to pass. Interviewers notice the order, and the narration above is what makes the order visible. If you are used to reasoning quietly, treat the sentences as part of the exercise, not decoration.

Unit testing follow-ups that come after the cycle

Stub or mock for a payment client. A stub returns canned answers; a mock also records and asserts how it was called. If your code only needs the client to return "approved", use a stub: the test then describes your code's behaviour, not the client's call sequence. Reach for a mock when the call itself is the behaviour you must guarantee, such as charging exactly once. Prefer the stub by default because mocks couple the test to internals and break on refactors that change nothing observable.

Coverage. A high percentage is not the goal. The answer that lands: "I'd rather have one test per behaviour than chase a number." A line can be executed without its result being checked.

Brittle tests. A test that breaks on every refactor usually asserts on internals. Fix it by asserting on observable output or on the public interface, so a rename inside the function cannot fail it.

Test naming. test_sum_positive_ignores_negatives survives a refactor because it names the behaviour. test_sum_1 names the order you happened to write things in, and tells the next reader nothing.

When TDD is the wrong tool, and how to say so

Test-first is a poor fit for exploratory spikes, throwaway scripts and UI layout work where the feedback loop is visual. Say so with structure: name the context, the decision and what you did instead. "On a spike I wrote the code first, then added tests once the shape settled, because I did not yet know what the interface should be." That is a judgement, not a confession.

What not to claim is that you always practise TDD if you do not. The follow-up is often "tell me about a time a test caught a bug," so have one real example ready: what the test asserted, what it caught, and what changed afterwards. For more on that style of questioning, see software testing concepts and trade-offs and reasoning about edge cases in coding interviews.

If you have never done TDD in a job

Say it plainly: "My last team tested after the fact. I've been practising test-first on my own." Then back it with evidence you can show: a small repository or a kata you did test-first, described by what failed first and what you changed. Describe the reasoning you would apply on their codebase and where you would start.

Here is an illustrative example, not a real candidate:

On a mid-sized product team, we wrote tests after the code and shipped on a weekly cycle. I wanted to try test-first, so I worked through a small kata on my own time: a shopping basket total. My first test failed with an AttributeError because the class did not exist. I made it pass with a hardcoded zero, then added a test for one item and wrote the real total. By the end I had a test for an empty basket, one item and a discount, and the discount test failed first with a wrong total, which showed me the discount logic was missing. In the interview I would describe that kata and the failing messages, not claim team experience I do not have.

What makes it work: it names the setting, the decision and the evidence, and it does not dress a personal exercise as a job. The follow-up it invites is "what would you do differently?", so think of one honest answer.

The first two minutes of a TDD exercise

Use this checklist before you type anything substantial:

  1. Read the spec and restate the smallest behaviour it asks for.
  2. Write the smallest test for that behaviour: one input, one expected output.
  3. Run it and confirm it fails.
  4. Say the failure message out loud and why it is the right failure.
  5. Plan the next test before you write the implementation.

That habit, running the test first every time, is what the format is checking. For the conversational side of the same exercise, answering technical questions out loud covers pacing and narration.

The one rule to take into the room

Run the test before you write the code, every time, and narrate the failure. That single habit is what TDD interview questions are built to reveal, and it is more convincing than any definition you can recite. If you have never done it on a job, say so and show a kata you did test-first. If you freeze, write the smallest test you can and run it. The order of your edits is the answer.

Frequently asked questions

Do I have to use TDD in a take-home test if the brief doesn't say so?

No, unless the brief asks for it. A take-home is judged on working code and clear tests, and writing tests first is one way to get there, not a requirement. If you do work test-first, mention it in your notes and describe what failed first, because that is the part a reviewer can check.

What if the interviewer asks me to skip the failing step to save time?

Follow the instruction, then say what you are giving up: "Skipping the red step means we won't see the test fail for the right reason." Interviewers sometimes test whether you can adapt. Do as asked, keep the test, and note that you would normally run it first so the omission is a choice, not an oversight.

How many tests should I write before the interviewer stops me?

Keep going until the behaviour in the spec is covered or the interviewer redirects you. There is no target count. A useful rhythm is one test per behaviour, adding edge cases such as an empty list or a single negative only when they change what the code must do. Say what you would test next if time runs out.

Is TDD different for frontend or mobile code?

The cycle is the same, but the feedback loop is often slower and more visual. Test logic and state transitions first, and treat layout as something to verify by eye or with snapshot checks. Say which parts you would drive test-first and which you would check visually, because that judgement is what the question is probing.

What if I disagree with the interviewer about whether a test is worth writing?

Say your reasoning once, briefly, then follow their call. Name the cost: "I'd test this because the rounding is easy to get wrong; if you'd rather move on, I'll note it as a gap." Disagreement handled that way shows judgement. Arguing past one exchange costs you more than the missing test.

Practising a test-first cycle? See how AI Interview assistant works.

  • Tdd interview questions
  • Test driven development interview
  • Unit testing interview questions
  • Coding interview
  • Software testing
A test bench with a laptop, phone and tablet side by side running app screens, a checklist and a magnifying glass

12 min read

Software Testing Interview Questions: Choose and Defend

Answer software testing interview questions by naming what you would test, what you would skip and why. Includes a 60-second login example and a practice routine.

  • Software testing interview questions
  • Software testing interview answers
Two engineers pair-programming at one laptop, one typing while the other points at the code on screen

14 min read

Software Engineer Interview Questions: Live Answer Frameworks

A practical guide to software engineer interview questions, with real-time frameworks for behavioural, technical and mixed answers you can adapt while speaking.

  • Software engineer interview questions
  • Software engineer interview answers
A developer workstation with two monitors, one showing a web interface mockup and the other code and a server diagram

12 min read

Full-Stack Development Interview Questions: Explain and Defend

A spoken three-beat structure for full-stack development interview questions: define the concept, contrast it with the alternative, and choose with a real constraint.

  • Full stack development interview questions
  • Full stack development interview answers