Program to find average waiting time in Python

Suppose we have an array customers, where customers[i] = [arrival_i, time_i]. Here arrival_i is the arrival time of the ith customer and time_i is the time needed to prepare the order. The cook processes orders sequentially and can only prepare one order at a time. We need to find the average waiting time of all customers.

The waiting time for each customer is the total time from arrival until their order is completed.

Problem Understanding

Let's understand with an example. If the input is customers = [[7,2],[8,4],[10,3],[20,1]], then ?

  • Customer 1 arrives at time 7, cook starts immediately and finishes at time 9. Waiting time = 9 - 7 = 2

  • Customer 2 arrives at time 8, cook starts at time 9 (when free) and finishes at time 13. Waiting time = 13 - 8 = 5

  • Customer 3 arrives at time 10, cook starts at time 13 and finishes at time 16. Waiting time = 16 - 10 = 6

  • Customer 4 arrives at time 20, cook starts immediately and finishes at time 21. Waiting time = 21 - 20 = 1

Average waiting time = (2 + 5 + 6 + 1) / 4 = 3.5

Algorithm

The approach is to track when the cook becomes free and calculate waiting times ?

  • Initialize current_time to track when cook is free
  • For each customer, if they arrive after cook is free, cook starts immediately
  • Otherwise, customer waits until cook is available
  • Calculate waiting time as completion time minus arrival time

Implementation

def solve(customers):
    waiting_times = []
    current_time = 0
    
    for arrival, prep_time in customers:
        # If customer arrives after cook is free, cook starts immediately
        if arrival > current_time:
            current_time = arrival + prep_time
        else:
            # Customer waits, cook starts when available
            current_time += prep_time
        
        # Waiting time = completion time - arrival time
        waiting_times.append(current_time - arrival)
    
    return sum(waiting_times) / len(waiting_times)

customers = [[7,2],[8,4],[10,3],[20,1]]
print(f"Average waiting time: {solve(customers)}")
Average waiting time: 3.5

Step-by-Step Execution

def solve_with_details(customers):
    waiting_times = []
    current_time = 0
    
    for i, (arrival, prep_time) in enumerate(customers, 1):
        if arrival > current_time:
            current_time = arrival + prep_time
            print(f"Customer {i}: Arrives at {arrival}, cook starts immediately")
        else:
            print(f"Customer {i}: Arrives at {arrival}, waits until {current_time}")
            current_time += prep_time
        
        waiting_time = current_time - arrival
        waiting_times.append(waiting_time)
        print(f"  Completes at {current_time}, waiting time = {waiting_time}")
    
    avg_waiting_time = sum(waiting_times) / len(waiting_times)
    print(f"\nAverage waiting time: {avg_waiting_time}")
    return avg_waiting_time

customers = [[7,2],[8,4],[10,3],[20,1]]
solve_with_details(customers)
Customer 1: Arrives at 7, cook starts immediately
  Completes at 9, waiting time = 2
Customer 2: Arrives at 8, waits until 9
  Completes at 13, waiting time = 5
Customer 3: Arrives at 10, waits until 13
  Completes at 16, waiting time = 6
Customer 4: Arrives at 20, cook starts immediately
  Completes at 21, waiting time = 1

Average waiting time: 3.5

Conclusion

The algorithm tracks the cook's availability and calculates waiting time as the difference between completion time and arrival time. The time complexity is O(n) where n is the number of customers.

Updated on: 2026-03-26T14:17:39+05:30

852 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements