Cake order fulfillment check
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google and Amazon. It tests simulation, arrays and inventory.
A bakery keeps three kinds of cake. Its stock is an array stock of length 3, where stock[i] is how many cakes of type i are on hand.
Orders arrive one at a time in the list orders. Each order is also an array of length 3: order[i] is how many cakes of type i the customer wants.
Serve the orders in the order they arrive. An order is fully completed only if the current stock covers every type it asks for. When that happens, remove all of its cakes from stock. If any type is short, reject the whole order: nothing is taken from stock, and you move on to the next order.
Implement:
def process_orders(stock: list[int], orders: list[list[int]]) -> list[bool]
Return one boolean per order, True if it was fully completed. Also be ready to return just the count of completed orders.
Constraints
0 <= stock[i] <= 10^90 <= len(orders) <= 10^5- Every order has length 3 and
0 <= order[i] <= 10^9
Example 1
stock = [5, 6, 7]
orders = [[2, 1, 3], [1, 1, 5], [3, 5, 4]]
- Order 0 fits. Stock becomes
[3, 5, 4]. - Order 1 needs 5 of type 2, but only 4 are left. Rejected, stock stays
[3, 5, 4]. - Order 2 fits exactly. Stock becomes
[0, 0, 0].
Output: [True, False, True] (2 orders completed).
Example 2
stock = [4, 4, 4]
orders = [[1, 1, 1], [1, 1, 9], [3, 3, 3]]
Output: [True, False, True]. The second order must not take any type-0 or type-1 cakes before it fails on type 2; otherwise the third order would wrongly be rejected.
Clarifying questions
- Is an order of all zeros completed (
True) without changing the stock? - Should the function change the caller's
stocklist, or work on a copy? - Can an order list contain a row that isn't length 3, and should that raise an error?
- Is the order of processing always the arrival order, or could the bakery choose which orders to serve?
What a strong answer covers
- Copies the stock, then for each order checks every type first and subtracts only when all three fit. A rejected order leaves the stock unchanged.
- Explains the bug the second example guards against: subtracting type by type and stopping at the shortage would take cakes from a rejected order.
- O(len(orders)) time and O(1) extra space. The count of completed orders is the number of
Truevalues. - Edge cases: no orders, an empty stock, an order that uses up the stock exactly, and very large counts.
- Tests: both examples, an all-zero order, an order larger than the stock in one type only, and a check that the caller's list is not changed.
Follow-ups
- The bakery may choose which orders to serve to complete as many as possible (a much harder packing problem).
- Many cake types instead of three.
- Orders can be partially filled, and you report what was sent.
- Stock is restocked at given times between orders.
Where it's asked
What it tests
simulation · arrays · inventory
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

