TDD
Essence
Test-Driven Development is a practice in which the test is written before the code and development proceeds in short Red → Green → Refactor cycles:
- Red: write a minimal test for the new behaviour; it fails (or does not even compile).
- Green: write the simplest code that makes the test pass. Elegance does not matter yet.
- Refactor: improve the structure of code and tests without changing behaviour; tests stay green.
A cycle takes minutes. Tests here are not an "after the fact" check but a design tool: they force you to decide first what the API should look like and what counts as a correct result.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep