Average Waiting Time - Problem
Restaurant Queue Simulation
Imagine you're running a popular restaurant with a single talented chef who can only prepare one order at a time. Throughout the day, customers arrive at different times and place orders that take varying amounts of time to prepare.
You are given an array
โข arrival_i: When the i-th customer walks through the door
โข time_i: How long it takes to prepare their specific order
The chef follows a simple rule: first come, first served. When a customer arrives, they give their order to the chef. If the chef is busy, they wait. If the chef is free, preparation starts immediately.
Goal: Calculate the average waiting time across all customers. A customer's waiting time is the total time from when they arrive until their food is ready.
Note: Arrival times are given in non-decreasing order, and solutions within 10-5 of the actual answer are accepted.
Imagine you're running a popular restaurant with a single talented chef who can only prepare one order at a time. Throughout the day, customers arrive at different times and place orders that take varying amounts of time to prepare.
You are given an array
customers where each element customers[i] = [arrival_i, time_i] represents:โข arrival_i: When the i-th customer walks through the door
โข time_i: How long it takes to prepare their specific order
The chef follows a simple rule: first come, first served. When a customer arrives, they give their order to the chef. If the chef is busy, they wait. If the chef is free, preparation starts immediately.
Goal: Calculate the average waiting time across all customers. A customer's waiting time is the total time from when they arrive until their food is ready.
Note: Arrival times are given in non-decreasing order, and solutions within 10-5 of the actual answer are accepted.
Input & Output
example_1.py โ Basic Case
$
Input:
customers = [[1,2],[2,5],[4,3]]
โบ
Output:
5.00000
๐ก Note:
Customer 1: arrives at 1, chef starts immediately, finishes at 3, wait = 2. Customer 2: arrives at 2, chef busy until 3, starts at 3, finishes at 8, wait = 6. Customer 3: arrives at 4, chef busy until 8, starts at 8, finishes at 11, wait = 7. Average = (2+6+7)/3 = 5.0
example_2.py โ No Waiting
$
Input:
customers = [[5,2],[5,4],[10,3],[20,1]]
โบ
Output:
3.25000
๐ก Note:
Customer 1: arrives 5, starts 5, finishes 7, wait = 2. Customer 2: arrives 5, starts 7, finishes 11, wait = 6. Customer 3: arrives 10, starts 11, finishes 14, wait = 4. Customer 4: arrives 20, starts 20, finishes 21, wait = 1. Average = (2+6+4+1)/4 = 3.25
example_3.py โ Single Customer
$
Input:
customers = [[2,3]]
โบ
Output:
3.00000
๐ก Note:
Only one customer arrives at time 2, chef starts immediately, finishes at time 5. Wait time = 5 - 2 = 3. Average = 3.0
Visualization
Tap to expand
Understanding the Visualization
1
Customer Arrives
Customer enters and places order
2
Check Chef Status
Is chef free or busy with previous order?
3
Start Cooking
Chef begins preparing when available
4
Calculate Wait
Total time from arrival to food ready
Key Takeaway
๐ฏ Key Insight: Track the chef's next available time and simulate each customer's journey from arrival to completion in a single efficient pass.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all customers
โ Linear Growth
Space Complexity
O(1)
Only using constant extra variables
โ Linear Space
Constraints
- 1 โค customers.length โค 105
- 1 โค arrivali, timei โค 104
- 0 โค arrivali โค arrivali+1
- Arrival times are sorted in non-decreasing order
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code