Problemwarehouse inventory

Session: Sign in to solve

Solution.txt

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. addProduct increases 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 shipOrder succeeds only when every requested copy is available.
  • Ship all requested copies. A successful shipOrder removes exactly the requested copies and returns true.
  • Keep failed orders atomic. An unsuccessful shipOrder returns false without changing any product count.

API

SignatureReturnsBehavior
WarehouseInventory()Not applicableCreates an empty inventory.
addProduct(productId: string)integerAdds one copy and returns the product's new count.
shipOrder(productIds: string[])booleanAttempts to ship the complete order.

Examples

Starting from a new inventory:

OperationResult
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:

OperationResult
addProduct("lamp")1
addProduct("rug")1
shipOrder(["lamp", "lamp", "rug"])false
shipOrder(["lamp", "rug"])true

Constraints

  • Each productId is a non-empty string of at most 64 characters.
  • productIds contains 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.