Cake order fulfillment check

0
Sign in

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^9
  • 0 <= 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.

Reconstructed from the reported question; details may differ from what interviewers ask.

GoogleAmazonCodingEasy

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.