Compute delivery wait times
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates competence in event-driven simulation, scheduling and queueing concepts, and algorithmic analysis by requiring computation of average customer waiting time for single and multiple shoppers.
Read the full Instacart Software Engineer interview experience this question came from
Delivery Wait Times — Single Shopper
Constraints
- 0 <= n <= 10^5
- arrival_time is non-decreasing
- service_time[i] >= 1
- Round the average to 5 decimal places
- Return 0.0 when there are no orders
Examples
Input: ([0, 1, 2], [3, 3, 3])
Expected Output: 5.0
Explanation: Order0: 0->3 (wait 3). Order1 starts at 3: 3->6 (wait 5). Order2 starts at 6: 6->9 (wait 7). Total 15, avg 5.0.
Input: ([0, 5, 10], [2, 2, 2])
Expected Output: 2.0
Explanation: Shopper is idle between orders, so each order starts at its own arrival and waits exactly its service time (2).
Hints
- Track the time at which the shopper becomes free; the next order can only start at max(free_time, its_arrival).
- waiting_time already includes the queueing delay because it is measured from arrival_time, not from start_time.
- Accumulate finish_time - arrival_time per order, then divide by n and round to 5 decimals.
Delivery Wait Times — k Shoppers
Constraints
- 1 <= k <= 10^5
- 0 <= n <= 10^5
- arrival_time is non-decreasing
- service_time[i] >= 1
- Assign each order to the earliest-available shopper; break ties by smallest shopper id
- Round the average to 5 decimal places
- Return 0.0 when there are no orders
Examples
Input: ([0, 1, 2], [3, 3, 3], 1)
Expected Output: 5.0
Explanation: With one shopper this matches Part 1: waits 3, 5, 7 -> avg 5.0.
Input: ([0, 1, 2], [3, 3, 3], 3)
Expected Output: 3.0
Explanation: Three shoppers, each takes one order with no queueing: every order waits exactly its 3-unit service time -> avg 3.0.
Hints
- Keep a min-heap of (available_time, shopper_id). Popping gives the earliest-available shopper, with the id as a natural tie-breaker.
- The order's start time is max(that shopper's available_time, the order's arrival_time); push back (start + service_time, shopper_id).
- Because orders are processed in arrival order, you never need to sort — just iterate and let the heap pick the shopper.
- k = 1 reduces exactly to the single-shopper version.