1701. Average Waiting Time - Explanation

Problem Link



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Simulation - Processing events in order and tracking state over time
  • Linear Array Traversal - Iterating through customers in arrival order
  • Basic Arithmetic - Calculating wait times using max operations and running totals

1. Simulation - I

Intuition

We simulate the chef serving customers one by one. The chef can only start a new order when they finish the previous one. If a customer arrives while the chef is busy, they wait. The waiting time for each customer is the time from arrival until their order is complete. We track the current time and accumulate total waiting time.

Algorithm

  1. Initialize t (current time) and total (total waiting time) to 0.
  2. For each customer with arrival time and order duration:
    • If the chef is still busy (t > arrival), add the extra wait to total.
    • Otherwise, update t to the arrival time.
    • Add the order time to both total and t.
  3. Return total divided by the number of customers.
class Solution:
    def averageWaitingTime(self, customers: List[List[int]]) -> float:
        t = 0
        total = 0

        for arrival, order in customers:
            if t > arrival:
                total += t - arrival
            else:
                t = arrival
            total += order
            t += order

        return total / len(customers)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

2. Simulation - II

Intuition

This is a more concise version of the simulation. The key observation is that the chef starts cooking at whichever is later: their current finish time or the customer's arrival. The finish time becomes max(t, arrival) + order. The customer's waiting time is simply finish_time - arrival. This formula captures both cases (chef idle or busy) in one expression.

Algorithm

  1. Initialize t (finish time) and total to 0.
  2. For each customer:
    • Update t to max(t, arrival) + order (when the order finishes).
    • Add t - arrival to total (customer's wait time).
  3. Return total divided by the number of customers.
class Solution:
    def averageWaitingTime(self, customers: List[List[int]]) -> float:
        t = total = 0
        for arrival, order in customers:
            t = max(t, arrival) + order
            total += t - arrival
        return total / len(customers)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

Common Pitfalls

Integer Overflow

With large arrival times and order durations, the sum of waiting times can exceed 32-bit integer limits. Use 64-bit integers for accumulating totals.

// Wrong: int can overflow
int total = 0;
// Right: use long for accumulation
long total = 0;

Confusing Waiting Time with Service Time

Waiting time is from arrival until the order is complete (includes cooking time), not just the time spent waiting before cooking starts.

# Wrong: only counting idle wait time
wait = max(0, t - arrival)
# Right: total time from arrival to completion
wait = (max(t, arrival) + order) - arrival

Forgetting to Update Current Time

When a customer arrives after the chef is idle, the current time must jump forward to the arrival time, not stay at the previous finish time.

# Wrong: always adding order time to current t
t += order
# Right: chef starts at max(t, arrival)
t = max(t, arrival) + order