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 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
๐Ÿณ Restaurant Simulation Timeline0510152025C1 arrivest=1Chef cooks (2 min)Wait: 3 minC2 arrivest=2Chef cooks (5 min)Wait: 6 min๐Ÿ‘จโ€๐Ÿณ Chef (processes orders sequentially)Average Wait = (3 + 6 + ...) / n
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค customers.length โ‰ค 105
  • 1 โ‰ค arrivali, timei โ‰ค 104
  • 0 โ‰ค arrivali โ‰ค arrivali+1
  • Arrival times are sorted in non-decreasing order
Asked in
Amazon 15 Microsoft 12 Google 8 Apple 5
28.0K Views
Medium Frequency
~12 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