Inventory Reservation
The checkout hold every store needs: reservations fence off stock without shipping it, so two carts can't oversell the last unit.
Requirements
- Track two independent quantities per SKU: physical on-hand stock and reserved holds. Sellable available is always
on-hand − reservedand is never allowed to go negative. reserveis all-or-nothing: it succeeds only whenavailable ≥ qty, and on success it grows the hold (it does not decrement on-hand — a reservation is a promise, not a shipment).releasereturns held units to the sellable pool by shrinking the reserved count, clamped at 0 — releasing more than is held must not create negative reservations.availableon a SKU that was never stocked returns 0, not an error.
API to implement
Implement the class Inventory. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| addStock <sku> <qty> | — | Add qty physical units of sku to on-hand stock. Called with qty ≥ 1; on-hand accumulates across calls. |
| reserve <sku> <qty> | boolean | If available (on-hand minus already-reserved) is at least qty, place a hold of qty and return true; otherwise change nothing and return false. |
| release <sku> <qty> | — | Cancel a hold: reduce the reserved amount by qty, clamped so it never drops below 0. On-hand stock is unaffected. |
| available <sku> | int | Return sellable units = on-hand minus reserved. An unknown SKU returns 0. |
Input format
The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
6
addStock apple 10
reserve apple 4
available apple
reserve apple 7
release apple 2
available appletrue
6
false
86
available ghost
release ghost 5
available ghost
addStock x 3
reserve x 3
available x0
0
true
0