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.

Watch lesson4:28

Batches

First to expire goes first

The point: once pieces differ, two counts are too few. The line gets batches, and the change stays inside the line.

When jars have a best-before date

StockLine- batches: List<Batch>+ available(today): int+ hold(qty, today): PartsBatch+ code: String+ bestBefore: Date- onHand: int- held: int

1/4The counts move down

A line no longer has two numbers. It has batches, and each batch has two numbers of its own.

owns (dies together)usesimplements

Rule

FIFO and FEFO are not the same

BatchReceivedBest beforeFIFO takesFEFO takes
A1 May1 Decemberfirstsecond
B9 May1 Augustsecondfirst
B came later and ends sooner. First in, first out would let it go bad on the shelf.

Code

Hold across batches

Python
from dataclasses import dataclassfrom datetime import date, timedelta@dataclassclass Batch:    code: str    best_before: date    on_hand: int    held: int = 0    @property    def free(self):        return self.on_hand - self.heldclass DatedLine:    def __init__(self, batches, min_days):        self.batches = {b.code: b for b in batches}        self.min_days = min_days                  # the customer must have this many days left    def sellable(self, today):        limit = today + timedelta(days=self.min_days)        good = [b for b in self.batches.values() if b.free > 0 and b.best_before >= limit]        return sorted(good, key=lambda b: (b.best_before, b.code))    def hold(self, qty, today):        parts = []        for b in self.sellable(today):            take = min(qty, b.free)            parts.append((b.code, take))            qty -= take            if qty == 0:                break        if qty or not parts:            return None        for code, n in parts:            self.batches[code].held += n        return partstoday = date(2026, 7, 1)line = DatedLine([Batch("A", date(2026, 12, 1), 5), Batch("B", date(2026, 8, 1), 3),                  Batch("C", date(2026, 7, 5), 9)], min_days=14)assert line.hold(4, today) == [("B", 3), ("A", 1)]assert line.hold(5, today) is None                # 4 of A are left, C is too old to sellassert line.batches["A"].held == 1                # a refusal changed nothingprint("ok")
Nine jars of C are on hand and not available. So available now depends on the day, and cannot be a stored number.
Edit and run it in your browser

Consequences

What else changes

  • Time moves the count

    At midnight available may fall without a sale. The low bell needs a daily visit.

  • Recall

    Block batch B: hold everything that is free, list who got the rest.

  • Promise or piece

    Choose the batch late, when packing, if it only matters that one exists.

  • Returns

    A returned jar goes to its batch, or to a place called check.

Does a reservation name the batch?

BreaksNever
How
Holds are on the line, batches only on the shelf.
Limit
Two promises may count on the last good batch, and the packer finds only the old one.
Choose when
Never, with dates.
WorksAt reserve
How
Pick has a batch code from the start.
Limit
When a promise ends, its early batch was blocked for nothing, and later orders took later ones.
Choose when
Few expiries, or when a customer chooses the batch.
StrongAt packing, with a safe count
How
Reserve counts against batches that are still good on the shipping day. Packing takes the earliest.
Limit
Two counts to keep in step.
Choose when
Many promises, many expiries: food and medicine.