Watch 10 video solutions for Average Waiting Time, a medium level problem involving Array, Simulation. This walkthrough by NeetCodeIO has 7,844 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:
arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.timei is the time needed to prepare the order of the ith customer.When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.
Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.
Example 1:
Input: customers = [[1,2],[2,5],[4,3]] Output: 5.00000 Explanation: 1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2. 2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6. 3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7. So the average waiting time = (2 + 6 + 7) / 3 = 5.
Example 2:
Input: customers = [[5,2],[5,4],[10,3],[20,1]] Output: 3.25000 Explanation: 1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2. 2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6. 3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4. 4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1. So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
Constraints:
1 <= customers.length <= 1051 <= arrivali, timei <= 104arrivali <= arrivali+1Problem Overview: Each customer arrives at a specific time and needs a fixed amount of cooking time. The chef can handle only one order at a time. You process customers in arrival order and compute how long each customer waits from arrival until their food is finished. The goal is to return the average waiting time across all customers.
Approach 1: Straightforward Simulation (O(n) time, O(1) space)
This problem naturally maps to a simple simulation of the restaurant timeline. Maintain a variable currentTime representing when the chef becomes free. Iterate through the array of customers in order. For each customer, the chef starts cooking at max(currentTime, arrivalTime) because the chef may be idle if the next customer arrives later. The order finishes at startTime + cookTime, and the waiting time for that customer is finishTime - arrivalTime. Accumulate the waiting times and update currentTime to the finish time before moving to the next customer.
This works because the problem guarantees customers are already processed in arrival order. No sorting or complex data structures are required. The algorithm performs one pass through the list, making it O(n) time with constant extra space. This is essentially a direct simulation of the chef's workflow.
Approach 2: Event Simulation with Priority Queue (O(n log n) time, O(n) space)
A more general way to model the system is event-driven simulation. Instead of assuming sequential processing, treat each arrival and cooking completion as events. Store pending customers in a priority queue ordered by arrival time or availability. When the chef becomes free, pop the next customer and schedule the cooking completion event. Each event updates the global clock and contributes to the waiting time calculation.
This design is useful when extending the system to multiple chefs or more complex scheduling policies. The priority queue keeps the next event accessible in O(log n) time. While this approach is more flexible, it introduces unnecessary overhead for the single-chef constraint in this problem.
Recommended for interviews: The straightforward simulation is the expected solution. It shows that you can translate a real-world process into code and reason about time progression with a simple invariant: the chef starts the next order when both the chef and the customer are available. Mentioning the event-driven model can demonstrate deeper systems thinking, but the O(n) simulation is the cleanest and most efficient approach.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Straightforward Simulation | O(n) | O(1) | Best choice for the problem since customers are already ordered by arrival time |
| Event Simulation with Priority Queue | O(n log n) | O(n) | Useful when modeling more complex scheduling such as multiple chefs or dynamic events |