KatInterview

Order Fulfillment Engine with Backorder Queue

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-mapqueuesimulation

Last updated

Process a stream of orders and restocks against live inventory, fulfilling backorders in arrival order before replenishing on-hand stock.

Order Fulfillment Engine with Backorder Queue

You are building the core of a warehouse fulfillment system for an online marketplace. The system receives a time-ordered stream of events and must track inventory in real time.

Inputs

initial_stock — a list of (sku: str, qty: int) pairs representing on-hand inventory at startup.

events — a list already sorted by timestamp. Each event is one of:

  • ("order", order_id: str, items: list[(sku, qty)]) — a customer order requesting one or more SKUs.
  • ("restock", items: list[(sku, qty)]) — a warehouse replenishment adding stock.

Processing rules

On an Order event:

  • For each SKU in the order, allocate as many units as currently available.
  • If supply is short, fulfill what you can and record the remainder as a backorder attached to that order.
  • An order for a SKU with zero on-hand inventory is fully backordered.

On a Restock event:

  • Before adding anything to on-hand inventory, use the incoming stock to satisfy outstanding backorders in FIFO order (oldest order first) for each SKU.
  • Only after all backorders for a SKU are cleared does surplus stock go to on-hand inventory.
  • FIFO priority is per SKU: a restock of SKU A serves the oldest backorder waiting for A, regardless of other SKUs on that same order.

Output

Return a tuple (order_report, final_inventory):

  • order_report: a dict keyed by order_id, each value containing:
    • "items": a dict keyed by SKU with {"fulfilled": int, "backordered": int}
    • "fully_fulfilled": True if no quantity remains backordered after all events
  • final_inventory: a dict of {sku: qty} for all SKUs, sorted by SKU

Constraints

  • Up to 10⁵ events and 10⁵ distinct SKUs.
  • Target time complexity: O(L + S log S) where L is total line items across all events and S is the number of distinct SKUs.
  • Target space complexity: O(S + B) where B is the number of outstanding backorder lines.
  • The same SKU appearing more than once in a single order should be merged.
  • Non-positive quantities should be ignored.
  • Order IDs are unique.

What is expected

Describe the data structures you would choose and why, implement the solution, analyze time and space complexity, and walk through at least two edge cases. As a follow-up: how would you extend the design to support order cancellation without degrading throughput?

Hints

Hint 1

For O(1) allocation per line item, you need direct SKU lookup — think about what structure gives you that, and what structure lets you drain backorders in arrival order without scanning the whole queue.

Hint 2

A heap is tempting for FIFO but it's overkill here: events are already time-ordered, so insertion order is priority order. What does that let you simplify?

Hint 3

For cancellation, deleting from the middle of a queue is O(n). Think about how garbage collection patterns like lazy deletion can let you mark an entry as void and skip it at pop time, keeping dequeue cost amortized O(1).