FunctionRacerProblemsRace

Warehouse With Held Orders

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class Warehouse that tracks stock on a shelf, where units can be held for an order before they ship. Units of an item are available when they are on the shelf and not held for an active order. Write these methods:

Every call to hold uses a new order number. An order is active only after a hold that returned true, and only until it is released or shipped. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

Warehouse()
restock("bolt", 5) returns 5
hold(1, "bolt", 3) returns true
count_available("bolt") returns 2
hold(2, "bolt", 3) returns false
hold(3, "bolt", 2) returns true
ship(1) returns 3
release(3) returns 2
count_available("bolt") returns 2
ship(1) returns 0

Order 1 holds 3 of the 5 bolts, so only 2 are available and order 2 fails. Order 3 takes the last 2. Shipping order 1 removes its 3 bolts from the shelf, and releasing order 3 frees 2. Order 1 is no longer active, so shipping it again gives 0.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems