Compute the average order wait time for a single shopper

Quick Overview

Simulate one shopper who fulfills orders one at a time in order of arrival, and return the truncated average time from when each order is placed until it is done. It tests sorting with a stable tie rule, tracking when a single server becomes free, and avoiding integer overflow on large totals.

Compute the average order wait time for a single shopper

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

One shopper fulfills a list of orders. Each order is given as `[start_time, duration]`: it is placed at `start_time` and needs `duration` time units of the shopper's work. The shopper handles one order at a time, in order of start time. If the shopper is free when an order arrives, work on it begins immediately. If the shopper is still busy with an earlier order, the new order waits until the shopper finishes. The wait time of an order is the total time from when it was placed until it is completely fulfilled, that is `finish_time - start_time`. Return the average wait time across all orders as an integer, truncated toward zero. ### Function Signature ```python def average_wait_time(orders: list[list[int]]) -> int: ``` ### Rules - `orders[i] = [start_time, duration]`. The input list is not necessarily sorted by start time. - Orders that share a start time are processed in the order they appear in the input. - The shopper is free at time 0 and works on exactly one order at a time until it is finished. - The result is the integer part of the total wait divided by the number of orders. All values are positive, so this is the same as the floor. ### Constraints - `1 <= len(orders) <= 10^5` - `1 <= start_time, duration <= 10^4` ### Examples ```text Input: orders = [[1, 2], [2, 5], [4, 3]] Output: 5 ``` The first order runs from 1 to 3 (wait 2), the second waits for the shopper and runs from 3 to 8 (wait 6), and the third runs from 8 to 11 (wait 7). The total is 15, and 15 / 3 = 5. ```text Input: orders = [[5, 2], [5, 4], [10, 3], [20, 1]] Output: 3 ``` The finish times are 7, 11, 14 and 21, so the waits are 2, 6, 4 and 1, and the shopper sits idle from 14 to 20. The total is 13, and 13 / 4 = 3.25 truncates to 3. ```text Input: orders = [[3, 4], [1, 1], [3, 1]] Output: 3 ``` Order `[1, 1]` goes first and finishes at 2 (wait 1); the two orders placed at time 3 then run in input order, finishing at 7 (wait 4) and 8 (wait 5). The total is 10, which truncates to 3, while running the shorter order first would give a total of 7 and an answer of 2.

Overview: Simulate one shopper who fulfills orders one at a time in order of arrival, and return the truncated average time from when each order is placed until it is done. It tests sorting with a stable tie rule, tracking when a single server becomes free, and avoiding integer overflow on large totals.

Instacart logo
Instacart
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

One shopper fulfills a list of orders. Each order is given as [start_time, duration]: it is placed at start_time and needs duration time units of the shopper's work.

The shopper handles one order at a time, in order of start time. If the shopper is free when an order arrives, work on it begins immediately. If the shopper is still busy with an earlier order, the new order waits until the shopper finishes.

The wait time of an order is the total time from when it was placed until it is completely fulfilled, that is finish_time - start_time. Return the average wait time across all orders as an integer, truncated toward zero.

Function Signature

def average_wait_time(orders: list[list[int]]) -> int:

Rules

  • orders[i] = [start_time, duration] . The input list is not necessarily sorted by start time.
  • Orders that share a start time are processed in the order they appear in the input.
  • The shopper is free at time 0 and works on exactly one order at a time until it is finished.
  • The result is the integer part of the total wait divided by the number of orders. All values are positive, so this is the same as the floor.

Constraints

  • 1 <= len(orders) <= 10^5
  • 1 <= start_time, duration <= 10^4

Examples

Input: orders = [[1, 2], [2, 5], [4, 3]]
Output: 5

The first order runs from 1 to 3 (wait 2), the second waits for the shopper and runs from 3 to 8 (wait 6), and the third runs from 8 to 11 (wait 7). The total is 15, and 15 / 3 = 5.

Input: orders = [[5, 2], [5, 4], [10, 3], [20, 1]]
Output: 3

The finish times are 7, 11, 14 and 21, so the waits are 2, 6, 4 and 1, and the shopper sits idle from 14 to 20. The total is 13, and 13 / 4 = 3.25 truncates to 3.

Input: orders = [[3, 4], [1, 1], [3, 1]]
Output: 3

Order [1, 1] goes first and finishes at 2 (wait 1); the two orders placed at time 3 then run in input order, finishing at 7 (wait 4) and 8 (wait 5). The total is 10, which truncates to 3, while running the shorter order first would give a total of 7 and an answer of 2.