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: 3.00000
💡 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 Problem OverviewInput: Customer Orders[[1,2], [2,5], [4,3]][arrival, cook_time]Process: Sequential ServiceTrack chef availabilityCalculate wait timesOutput: Average Wait5.0(2+6+7) ÷ 3Customer 1Wait: 2Customer 2Wait: 6Customer 3Wait: 7Timeline:03811Average Waiting Time = Total Wait ÷ Number of Customers
Understanding the Visualization
1
Input
Array of [arrival_time, cook_time] for each customer
2
Process
Track chef availability and calculate waiting times
3
Output
Average of all customer waiting times
Key Takeaway
🎯 Key Insight: Track chef availability time and calculate each customer's total waiting time from arrival to service completion
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