Where would you introduce property-based testing, and where is it a poor fit?
Show the full answer Hide the answer
What the interviewer is testing
Whether you know what the technique is genuinely good at rather than treating it as universally superior.
Why it finds what example tests miss
Example-based tests check the cases the author thought of — which is precisely the set already handled correctly. Property-based testing states an invariant and has the framework hunt for a counterexample.
The feature that makes it practical is shrinking: when a failure is found, the input is reduced to the minimal failing case, so instead of a 400-element structure you get a two-element one and the bug is usually obvious.
Where it pays off
Parsers and serialisers — round-trip property: anything serialised then deserialised equals the original. Finds encoding, escaping and Unicode bugs immediately.
Financial arithmetic — invariants like "the ledger balance equals the sum of entries" under any sequence of operations. Finds rounding and ordering errors.
State machines — no sequence of valid transitions reaches an invalid state.
Concurrency and idempotency — applying an operation twice equals applying it once, which is directly relevant to retry-heavy distributed systems.
Reimplementations and optimisations — the equivalence property: the optimised version agrees with the naive one on every input. This is the strongest case of all.
Where it is a poor fit
Code whose behaviour is a list of business rules rather than a rule. "Customers in this segment get this discount unless it is a public holiday" has no invariant to state; the specification is the enumeration, and example-based tests express it directly.
Also poor where generating valid inputs is harder than the code under test, and where the property is so weak that it passes trivially.
What a strong answer adds
Property-based testing complements rather than replaces example tests: examples document intent and serve as specification, properties explore the space. And a found counterexample should be added as a permanent example test, so the specific regression is guarded even if generation does not reproduce it.
Common weak answers
Proposing it everywhere. Dismissing it as academic without naming a category where it applies.