Warehouse Inventory
Problem
A fulfilment warehouse receives products one copy at a time and must not send a partial order. Design WarehouseInventory to track the available count of each product and ship an order only when every requested copy is in stock.
Requirements
- Add one copy.
addProductincreases the named product's count by one and returns its new count. - Check the complete order. A product ID repeated in an order requests that many copies, and
shipOrdersucceeds only when every requested copy is available. - Ship all requested copies. A successful
shipOrderremoves exactly the requested copies and returnstrue. - Keep failed orders atomic. An unsuccessful
shipOrderreturnsfalsewithout changing any product count.
API
| Signature | Returns | Behavior |
|---|---|---|
WarehouseInventory() | Not applicable | Creates an empty inventory. |
addProduct(productId: string) | integer | Adds one copy and returns the product's new count. |
shipOrder(productIds: string[]) | boolean | Attempts to ship the complete order. |
Examples
Starting from a new inventory:
| Operation | Result |
|---|---|
addProduct("chair") | 1 |
addProduct("chair") | 2 |
addProduct("table") | 1 |
shipOrder(["chair", "table"]) | true |
shipOrder(["chair"]) | true |
shipOrder(["chair"]) | false |
A failed order does not remove the products that were available:
| Operation | Result |
|---|---|
addProduct("lamp") | 1 |
addProduct("rug") | 1 |
shipOrder(["lamp", "lamp", "rug"]) | false |
shipOrder(["lamp", "rug"]) | true |
Constraints
- Each
productIdis a non-empty string of at most 64 characters. productIdscontains between 1 and 1,000 entries.- At most 1,000 distinct product IDs and 10,000 public method calls occur in one testcase.
- Inputs satisfy these constraints.
Notes
Calls are sequential. Product IDs are compared as exact strings, and the order of IDs within productIds matters only through how often each ID occurs.