The rejected basket that consumes inventory
A failed multi-item reservation drains stock, while repeated basket lines can bypass the quantity check.
What’s going wrong
Original Amazon-style interview practice. This is not an actual Amazon interview question. Bugger does not provide an AI assistant.
An inventory service processes baskets one at a time. A rejected basket sometimes reduces stock anyway, causing a later valid order to fail. Baskets with the same SKU on multiple lines can also reserve fewer units than the customer requested. Repair the reservation boundary so each basket either consumes its complete quantity or changes nothing.
Input is {"stock":[{"sku":"book","quantity":5},...],"requests":[{"id":"r1","items":[{"sku":"book","quantity":2},...]},...]}. Initial stock entries have distinct SKU strings. Process requests in array order. Sum all quantities for a SKU within one request, then accept only if every required SKU has enough remaining stock for that summed quantity. On acceptance subtract all summed quantities. On rejection leave every SKU exactly unchanged. A missing SKU has zero available units and must not be inserted into stock. An empty basket is accepted without changing stock. Request IDs are distinct; these are separate orders, not retries.
Print {"results":[{"id":"r1","accepted":true,"remaining":[...]},...],"stock":[...]}. Each remaining array is a complete stock snapshot immediately after that request; stock is the final snapshot. Snapshots include every initial SKU, including zero-stock entries, sorted by ascending ASCII SKU. Valid input has at most 50 initial SKUs, 50 requests, and 50 lines per request. SKU and request IDs are nonempty ASCII strings up to 32 characters. Initial quantities are integers from 0 to 10000; requested line quantities are integers from 1 to 1000. Repair reserve without changing the input or output contracts.
Expected behavior
Aggregate repeated SKU quantities and commit a reservation only after every required quantity is available; rejection must preserve the entire stock state for subsequent orders.
Reproduction
{"stock":[{"sku":"a","quantity":3},{"sku":"b","quantity":0}],"requests":[{"id":"fail","items":[{"sku":"a","quantity":2},{"sku":"b","quantity":1}]},{"id":"next","items":[{"sku":"a","quantity":3}]}]}{"results":[{"id":"fail","accepted":false,"remaining":[{"sku":"a","quantity":3},{"sku":"b","quantity":0}]},{"id":"next","accepted":true,"remaining":[{"sku":"a","quantity":0},{"sku":"b","quantity":0}]}],"stock":[{"sku":"a","quantity":0},{"sku":"b","quantity":0}]}{"stock":[{"sku":"a","quantity":5},{"sku":"b","quantity":4}],"requests":[{"id":"ok","items":[{"sku":"a","quantity":2},{"sku":"b","quantity":3}]}]}{"results":[{"id":"ok","accepted":true,"remaining":[{"sku":"a","quantity":3},{"sku":"b","quantity":1}]}],"stock":[{"sku":"a","quantity":3},{"sku":"b","quantity":1}]}{"stock":[{"sku":"a","quantity":3}],"requests":[{"id":"over","items":[{"sku":"a","quantity":2},{"sku":"a","quantity":2}]},{"id":"exact","items":[{"sku":"a","quantity":3}]}]}{"results":[{"id":"over","accepted":false,"remaining":[{"sku":"a","quantity":3}]},{"id":"exact","accepted":true,"remaining":[{"sku":"a","quantity":0}]}],"stock":[{"sku":"a","quantity":0}]}Before you begin
- Dictionary aggregation
- Atomic state changes
- Tracing sequential transaction histories
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.