Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:
arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.timei is the time needed to prepare the order of the ith customer.When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.
Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.
Example 1:
Input: customers = [[1,2],[2,5],[4,3]] Output: 5.00000 Explanation: 1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2. 2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6. 3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7. So the average waiting time = (2 + 6 + 7) / 3 = 5.
Example 2:
Input: customers = [[5,2],[5,4],[10,3],[20,1]] Output: 3.25000 Explanation: 1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2. 2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6. 3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4. 4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1. So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
Constraints:
1 <= customers.length <= 1051 <= arrivali, timei <= 104arrivali <= arrivali+1Problem summary: There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith customer. When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input. Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2],[2,5],[4,3]]
[[5,2],[5,4],[10,3],[20,1]]
average-height-of-buildings-in-each-segment)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1701: Average Waiting Time
class Solution {
public double averageWaitingTime(int[][] customers) {
double tot = 0;
int t = 0;
for (var e : customers) {
int a = e[0], b = e[1];
t = Math.max(t, a) + b;
tot += t - a;
}
return tot / customers.length;
}
}
// Accepted solution for LeetCode #1701: Average Waiting Time
func averageWaitingTime(customers [][]int) float64 {
tot, t := 0, 0
for _, e := range customers {
a, b := e[0], e[1]
t = max(t, a) + b
tot += t - a
}
return float64(tot) / float64(len(customers))
}
# Accepted solution for LeetCode #1701: Average Waiting Time
class Solution:
def averageWaitingTime(self, customers: List[List[int]]) -> float:
tot = t = 0
for a, b in customers:
t = max(t, a) + b
tot += t - a
return tot / len(customers)
// Accepted solution for LeetCode #1701: Average Waiting Time
impl Solution {
pub fn average_waiting_time(customers: Vec<Vec<i32>>) -> f64 {
let mut tot = 0.0;
let mut t = 0;
for e in customers.iter() {
let a = e[0];
let b = e[1];
t = t.max(a) + b;
tot += (t - a) as f64;
}
tot / customers.len() as f64
}
}
// Accepted solution for LeetCode #1701: Average Waiting Time
function averageWaitingTime(customers: number[][]): number {
let [tot, t] = [0, 0];
for (const [a, b] of customers) {
t = Math.max(t, a) + b;
tot += t - a;
}
return tot / customers.length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.