Inventory Reservations with Expiry

medium
Unit 22 · Bisect0/0 to milestone

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) reservations
  • reserve(sku, qty, ttl): returns a reservation id (1, 2, 3...) if enough is available, else raises ValueError; expires ttl seconds after creation
  • commit(res_id): turns an active reservation into a sale (stock decreases); committing an expired or unknown reservation raises KeyError
  • release(res_id): cancels an active reservation (no error if already gone)
Pro problem

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
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Inventory Reservations with Expiry · TechGradify