ORIGINAL COMPANY-CONTEXT PRACTICE
Reserve scarce stock without overselling
Original practice scenario: Design a hypothetical marketplace checkout for a product with ten units and heavy concurrent demand. Customers can abandon checkout, retry payment and cancel orders.
Written for independent practice using company context; this is not a verified question from a company interview.
A REASONED APPROACH
Model answer.
Separate stock reservation, payment and order confirmation into explicit states. The inventory invariant must hold under concurrent attempts and repeated callbacks, not just a successful checkout sequence.
Work through it
- Use an authoritative per-item quantity with atomic conditional reservation; assign each checkout attempt a stable reservation identity and expiration.
- Record payment outcomes idempotently and define compensation when payment succeeds after the reservation expires or fulfillment becomes impossible.
- Release expired or cancelled reservations through a recoverable process and reconcile inventory, orders and payments using durable state transitions.
Tradeoffs to discuss
- Long reservations reduce customer frustration but hide stock from other shoppers; short reservations increase availability while complicating slow payments.
Common pitfalls
- A client-side countdown is not an authoritative expiration decision.
- Do not refund or release twice when cancellation, timeout and payment notifications race with each other.