Senior · Design · Contracts

Extend the contract to accept signed stock adjustments without losing a nonnegative final-stock invariant. Decide whether validity applies after every row or only after all rows, and provide an example where those policies disagree. Preserve an explicit key comparer and checked arithmetic.

Think through your answer before opening the explanation.

Coding interviews

Reveal answer and reasoning

A defensible extension first chooses prefix-valid stock or final-valid stock. With initial stock zero, adjustments minus one then plus one are invalid under the first policy and valid under the second. Store the proposed result locally until its chosen validation point, and reject arithmetic overflow regardless of which business policy is selected.

For n rows and u distinct keys, aggregation uses expected O(n) dictionary operations and O(u) stored totals; string hashing also examines key text. Display sorting costs O(u log u) comparisons and additional sorting storage. Checked long arithmetic rejects overflow rather than silently changing an otherwise valid total. Case normalization, negative adjustments and blank-key handling are business decisions, not harmless implementation details.

Returning a partially filled dictionary after an exception would create a different contract. Local construction makes failure discard the unpublished result; it does not provide a database transaction or a thread-safe aggregation service.

A strong interview explanation starts with the exact-key and nonnegative-input contract, states the processed-prefix invariant, and then separates expected aggregation cost from sorted-display cost. It supports correctness with a duplicate-key example, a mixed-case example and an invalid row. A senior extension identifies the publication boundary and the finite-input assumption before discussing concurrency or persistence. No part of this console fixture reserves inventory, coordinates replicas or promises dictionary iteration order.

The useful deliverable is a small deterministic solution whose limits are as easy to inspect as its successful output.

What a strong answer covers

  • A defensible extension first chooses prefix-valid stock or final-valid stock.
  • For n rows and u distinct keys, aggregation uses expected O(n) dictionary operations and O(u) stored totals; string hashing also examines key text.
  • Case normalization, negative adjustments and blank-key handling are business decisions, not harmless implementation details.

Common pitfall

Case normalization, negative adjustments and blank-key handling are business decisions, not harmless implementation details. Returning a partially filled dictionary after an exception would create a different contract. Local construction makes failure discard the unpublished result; it does not provide a database transaction or a thread-safe aggregation service.

Follow-up questions

  1. Add zero-quantity rows and an empty key as separate cases. Predict that zero preserves the total while an empty key fails. Hint: validity and contribution are different questions, so do not use quantity greater than zero as the only admission rule.
  2. How would ordinal-ignore-case grouping change the sample and the displayed representative key policy?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions