Ctrl K

The Coffee Kiosk Queue

Medium
A coffee kiosk has a single barista. Order orders[i] = [arrival, time] means a customer arrives at moment arrival and their drink takes time minutes to make. Customers are listed in the order they arrive. The barista makes drinks strictly in turn, one at a time. If the barista is free when a customer arrives, work on their order starts right away; otherwise it starts as soon as the previous drink is done. A customer's waiting time runs from their arrival until their drink is ready. Return the average waiting time over all customers. Answers within 10⁻⁵ of the exact value are accepted.
Example 1
orders=[[2, 3], [3, 1], [10, 2]] → 2.6666666666666665

The first order is ready at 5 (a wait of 3), the second starts at 5 and is ready at 6 (a wait of 3), the third starts at 10 and is ready at 12 (a wait of 2). Average: (3 + 3 + 2) / 3 ≈ 2.6667.

Example 2
orders=[[1, 4], [1, 4]] → 6.0

Both arrive at 1: the first gets the coffee at 5 and the second at 9. Average: (4 + 8) / 2 = 6.

Constraints
  • 1 ≤ len(orders) ≤ 10⁵
  • 1 ≤ arrival, time ≤ 10⁴
  • Arrival times never decrease
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
orders=[[2, 3], [3, 1], [10, 2]]
Expected
2.6666666666666665

Run checks the first 3 tests in your browser. Submit checks all 91 tests on the server, hidden ones included.