FIFO Order Allocator
AirbnbOnsiteEasy
An order asks for quantity units of a product. Stock is held in batches listed from the oldest to the newest, each as [reference_id, available]: a unique string reference_id and the number of units still available in it.
Write allocate_order(quantity, batches).
Part 1: Oldest batches first
Fill the order first-in, first-out: take units from the oldest batch until it is empty, then from the next one, and so on until the order is complete or the stock is gone.
Return a map with two keys. "allocations" lists [reference_id, units] for every batch that gives at least one unit, in the order the batches are given. "unfulfilled" is the number of units the order still lacks after all batches are used, 0 when the order is complete.
A batch with 0 available is never listed, and an order of 0 units uses no batch.
- Example 1
- quantity
12- batches
[["A",5],["B",10],["C",8]]- Output
{"allocations":[["A",5],["B",7]],"unfulfilled":0}
Why: The oldest batch A gives all 5 units, leaving 7. The next batch B has 10, so it gives 7 and the order is complete. C is not used, so it does not appear, and nothing is unfulfilled.
Constraints
0 <= quantity <= 10^180 <= len(batches) <= 2 * 10^50 <= batches[i][1] <= 10^181 <= len(batches[i][0]) <= 20, and everybatches[i][0]is distinct