An online shop reserves stock when a customer starts checkout. Implement Inventory(clock) where clock() returns the current time in seconds (injected so it can be tested):
add_stock(sku, qty)available(sku): stock minus active (unexpired) reservationsreserve(sku, qty, ttl): returns a reservation id (1, 2, 3...) if enough is available, else raises ValueError; expires ttl seconds after creationcommit(res_id): turns an active reservation into a sale (stock decreases); committing an expired or unknown reservation raises KeyErrorrelease(res_id): cancels an active reservation (no error if already gone)Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.
Start 7-day free trial