AlgoViz

Design an inventory management system

medium 80 min

Count goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:28

See it run

Two counts for each line. Watch which one moves, and when.

Operations

Reserve an order

Plan both products, then hold both.

Step 1 of 7

Before

shopcallerinventoryInventoryopennonelockfreeplum cellarStockLineonHand6held0plum shedStockLineonHand3held0fig cellarStockLineonHand4held0

Plum: 6 in the cellar, 3 in the shed. Fig: 4. Nothing is held.

Step 1: Before

def reserve(self, order_id, wanted, seconds):    with self._lock:        picks = []        for sku, qty in wanted.items():            plan = self._policy.plan(qty, self._lines_of(sku))            if plan is None:                raise OutOfStock(sku)            picks.extend(plan)        for pick in picks:            self._lines[pick.sku, pick.place].hold(pick.qty)        res = Reservation(next(self._ids), order_id, tuple(picks),                          self._now() + seconds)        self._reservations[res.id] = res        news = self._low_news(picks)    self._tell(news)    return res
Cost
O(products × lines)
Guarantee
All or nothing

The upper half reads. The lower half writes.

In the real world

The same inventory doing real jobs. Pick one and step through it.

Five leave, four arrive.

Step 1 of 4

Seven in the cellar

cellarPlaceplum7road T1Placeplum0shedPlaceplum0lostPlaceplum0

Transfer T1 will carry five to the shed.

Step 1: Seven in the cellar

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Ask, then act

    critical

    Another order may come between the question and the change.

    Wrong
    if inventory.is_in_stock(sku, 1):    inventory.decrement(sku, 1)
    Right
    res = inventory.reserve(order_id, {sku: 1}, 900)
  • Holding while planning

    critical

    When the third product is missing, two are held by nobody.

    Wrong
    for sku, qty in wanted.items():    line(sku).hold(qty)      # may raise halfway
    Right
    picks = [plan(sku, qty) for ...]   # may raisefor pick in picks: hold(pick)      # cannot
  • Lowering available twice

    critical

    The jar was subtracted when it was held and again when it left.

    Wrong
    def ship(self): self.on_hand -= qty     # held stays
    Right
    def ship(self):    self.held -= qty    self.on_hand -= qty
  • A listener under the lock

    major

    A slow mail server stops every sale.

    Wrong
    with self._lock:    line.hold(qty)    mailer.send(...)
    Right
    with self._lock:    line.hold(qty)    news = ...tell(news)
  • A key made by joining text

    major

    ab + c and a + bc become the same line.

    Wrong
    lines[sku + place]
    Right
    lines[sku, place]
  • Trusting the sweeper

    major

    If it is late, an overdue promise still ships.

    Wrong
    if res.status is Status.HELD: ship()
    Right
    if res.status is Status.HELD and now < res.expires_at: ship()

Interview tips

  • Open with

    • On hand, held, available.
    • A line is product and place.
    • Reserve is the hard verb.
  • Reject aloud

    • Product is a key here.
    • Order is an id.
    • No Singleton.
  • Prove it

    • Last jar, two orders.
    • Half an order.
    • Second scan.
  • Never

    • A stored available.
    • A hold without an end.
    • Mail under the lock.