Coding interviews
Reveal answer and reasoning
A coding-review task often mixes validation, calculation, logging and persistence in one method. Extract a pure decision with explicit input and output before changing behavior. This sample validates quantity, price and stock, distinguishes insufficient stock from invalid input and computes a checked total only for approved input. Records describe data; they do not make a stock reservation or commit an order.
Write the observable decision table before extracting helpers. Valid quantity within stock gives an approved checked total; valid quantity beyond stock is insufficient; malformed numeric inputs are invalid. This table is the refactor's regression contract, while logging or storage adapters can be tested separately at their own boundaries.
The decision uses constant time and fixed-size result data. Record construction allocates small objects in this class-record version. Extracting a pure function improves deterministic testing but is not by itself a performance optimization. Overflow remains an explicit exceptional outcome for otherwise valid but unrepresentable totals. A pure function has no hidden service calls, but it can still throw, allocate and depend on its stated numeric representation.
Do not equate purity with zero cost or totality over every possible input. Here checked overflow remains a documented exceptional path requiring an explicit caller boundary.
What a strong answer covers
- A coding-review task often mixes validation, calculation, logging and persistence in one method.
- The zero total on insufficient or invalid decisions is a deliberate non-approved result convention.
- The decision uses constant time and fixed-size result data.
Common pitfall
Refactoring should preserve observable error precedence unless the contract intentionally changes. Checking stock first could classify a malformed negative-stock request differently. A successful decision is also not evidence that stock remains available when another caller later performs an external write.
Follow-up questions
- Add fixtures where several validation rules fail simultaneously and specify the desired precedence. Hint: assert decision Status and total, not an internal sequence of helper calls. Compare the refactor against a written behavior table.
- How should persistence respond if stock changes after the pure decision but before the write?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.