FIFO Order Allocator

Asked at AirbnbOnsite

Reported by the problem's creator, not verified

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).

Examples

  1. Example 1

    Input: {"quantity":12,"batches":[["A",5],["B",10],["C",8]]}

    Output: {"allocations":[["A",5],["B",7]],"unfulfilled":0}

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.