How should you derive test cases before coding an interview solution?
Answer
Restate the contract, ask about invalid input and constraints, and write one normal example plus empty, smallest, largest, duplicate and boundary cases that fit the problem. Use those examples to expose hidden assumptions and choose a data structure. After coding, trace the algorithm through the same cases and add one case aimed at its most fragile branch.
Strong answer includes
- clarifies the contract before implementation
- uses boundaries and constraints to select cases
- targets the algorithm's fragile branch after coding
Practical example
Before coding a function that finds the kth largest element, a candidate asks whether the array can contain duplicates and whether k is guaranteed to be within bounds; the interviewer says duplicates are allowed and k is unbounded, which changes the candidate's chosen approach to explicitly validate k before running any selection logic.