Inventory allocation with backorders
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round, last asked Jul 2025. It tests simulation, queues and hashing.
Process a stream of events for an online store's inventory. You start with some stock per SKU. Events arrive in time order and are either an order or a restock. An order takes what it can from current stock, and whatever is short becomes a backorder for that order. A restock first fills the outstanding backorders of each SKU, oldest first, and only the rest goes back on the shelf. After all events, report how each order was filled and the stock left.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
process(initial_inventory: list[tuple[str, int]], events: list) -> tuple[list, list]- an event is
("order", order_id, [(sku, qty), ...])or("restock", [(sku, qty), ...]). - returns
(order_details, final_inventory):-
order_details: one entry per order in first-seen order,(order_id, [(sku, fulfilled, backordered), ...], fully_fulfilled), lines sorted by SKU;fully_fulfilledis true when nothing is still backordered. -
final_inventory:[(sku, qty), ...]for SKUs withqty > 0, sorted by SKU.
-
- an event is
- An order id seen again adds to that order's lines; repeated SKUs in one order add up.
Example
initial = [("A", 1)]
events = [("order", "o1", [("A", 3)]),
("order", "o2", [("A", 2)]),
("restock", [("A", 3)])]
-> order_details = [("o1", [("A", 3, 0)], True),
("o2", [("A", 1, 1)], False)]
final_inventory = []
# o1 takes the 1 on hand; the restock fills o1's 2 first, then 1 of o2's 2
initial = [("A", 2), ("B", 0)]
events = [("order", "o1", [("A", 3), ("B", 1)]),
("restock", [("B", 2)]),
("order", "o2", [("A", 1)]),
("restock", [("A", 4)])]
-> order_details = [("o1", [("A", 3, 0), ("B", 1, 0)], True),
("o2", [("A", 1, 0)], True)]
final_inventory = [("A", 2), ("B", 1)]
Constraints
- Up to 10^5 events and 10^5 distinct SKUs; quantities are non-negative integers.
- Target O(E log S) time and O(S + B) space, for
SSKUs andBoutstanding backorder lines.
Clarifying questions
- Can an order be partly filled, or is it all or nothing?
- Is the backorder queue per SKU or per order? Can a restock fill part of one backorder?
- What does an unknown SKU in an order mean: zero stock, or an error?
- Are events guaranteed to be in time order?
What a strong answer covers
- A hash map of on-hand stock, a FIFO queue of
(order_id, remaining)per SKU, and per-order line totals. - Restock draining a SKU's queue front to back, splitting the last backorder when stock runs out; each backorder line is touched a constant number of times overall, amortised.
- Deterministic output order, and tests: restock with no backorders, a restock smaller than the oldest backorder, an order id seen twice.
Follow-ups
- Support cancelling an order in O(log S): its fulfilled units go back on the shelf (and to waiting backorders), and its backorders leave the queues.
- Priority customers' backorders are filled before older ones. What replaces the FIFO queue?
- Many warehouses: which one fills an order, and how do restocks route?
Where it's asked
- Amazon · Coding · Last asked Jul 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
simulation · queues · hashing
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
