Average Waiting Time - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: customers = [[1,2],[2,5],[4,3]]
Output: 5.00000
💡 Note: Customer 1: arrives at 1, served 1→3, waits 2 time units. Customer 2: arrives at 2, must wait until chef free at 3, served 3→8, waits 6 time units. Customer 3: arrives at 4, must wait until chef free at 8, served 8→11, waits 7 time units. Average = (2+6+7)/3 = 5.0
Example 2 — No Waiting
$ Input: customers = [[5,2],[5,4],[6,3]]
Output: 5.33333
💡 Note: Customer 1: arrives at 5, served immediately 5→7, waits 2 units. Customer 2: arrives at 5, waits until 7, served 7→11, waits 6 units. Customer 3: arrives at 6, waits until 11, served 11→14, waits 8 units. Average = (2+6+8)/3 = 16/3 ≈ 5.33
Example 3 — Single Customer
$ Input: customers = [[2,3]]
Output: 3.00000
💡 Note: Only one customer arrives at time 2, takes 3 time units to prepare, so waiting time is exactly 3.0

Constraints

  • 1 ≤ customers.length ≤ 105
  • 1 ≤ arrivali, timei ≤ 104
  • 0 ≤ arrivali ≤ arrivali+1

Visualization

Tap to expand
Average Waiting Time - Optimized Simulation INPUT customers array Customer 0 arr: 1 time: 2 Customer 1 arr: 2 time: 5 Customer 2 arr: 4 time: 3 Timeline View t=1 t=2 t=4 [[1,2],[2,5],[4,3]] ALGORITHM STEPS 1 Initialize currTime=0, totalWait=0 2 Process Each Customer Update currTime if idle 3 Calculate Wait Time wait = finishTime - arrival 4 Compute Average totalWait / n customers Simulation Trace C0: start=1, end=3, wait=2 C1: start=3, end=8, wait=6 C2: start=8, end=11, wait=7 Total: 2+6+7 = 15 avg = totalWait / n = 15/3 FINAL RESULT Average Wait Time 5.00000 Wait Times Breakdown 2 C0 6 C1 7 C2 Verification (2+6+7)/3 = 5.0 OK Output: 5.00000 Key Insight: Track chef's finish time (currTime). For each customer: if chef is idle (currTime < arrival), chef starts at arrival; otherwise chef starts when free. Wait time = finishTime - arrivalTime. No queue structure needed - O(n) time, O(1) space. Formula: currTime = max(currTime, arrival) + prepTime; waitTime = currTime - arrival TutorialsPoint - Average Waiting Time | Optimized Simulation Approach
Asked in
Amazon 15 Google 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen