Coding interviews
Reveal answer and reasoning
A robust test portfolio combines contract examples, independent oracles on small inputs and properties such as no source mutation. On failure, record the exact data and seed state, then reduce the case while preserving failure. This harness does not implement automatic shrinking, so label any manual reduction honestly rather than claiming a full property-testing framework.
Fast uses expected O(n) time and O(u) stored counts. Oracle can take O(n squared) time but requires constant auxiliary state. The slow method is intentionally retained only for small verification inputs, where simplicity and independence can be more valuable than speed. Two implementations sharing the same mistaken selection rule can agree while both are wrong. Generated tests therefore complement explicit examples such as empty input, all duplicates and a unique negative value.
Passing two hundred cases is bounded evidence, not an exhaustive proof or a benchmark.
A strong interview answer explains why the slow oracle is intentionally independent, why the finite generated domain is useful and why agreement is still bounded evidence. It distinguishes test strategy from the already familiar frequency-map algorithm. A senior review asks which wrong implementations the portfolio can reject, including smallest-unique selection and sentinel misuse. The sample's specified generator avoids dependence on framework random-sequence stability, while its exact success output makes execution easy to verify.
Useful testing work produces reproducible counterexamples and meaningful boundaries, not only a large number printed after two similar implementations agree. Trial zero is deliberately empty, so both functions must return null without treating zero as an absence sentinel. Explicit zero-valued unique fixtures protect that distinction independently of generation.
What a strong answer covers
- A robust test portfolio combines contract examples, independent oracles on small inputs and properties such as no source mutation.
- Fast uses expected O(n) time and O(u) stored counts.
- Two implementations sharing the same mistaken selection rule can agree while both are wrong.
Common pitfall
Two implementations sharing the same mistaken selection rule can agree while both are wrong. Generated tests therefore complement explicit examples such as empty input, all duplicates and a unique negative value. Passing two hundred cases is bounded evidence, not an exhaustive proof or a benchmark.
Follow-up questions
- Deliberately change Fast to return the smallest unique value and add a fixture whose first unique value is larger. Hint: the oracle must preserve the stated first-occurrence contract, not just membership in the set of unique values.
- What additional mechanism would turn this deterministic generator into a test system with shrinking?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.