Fix the batch timing model for a concurrency limit
Your task
An agent migrated a Node module from error-first callbacks to async/await. The original processBatch started every save at once. The new one awaits them one at a time in a loop, so a batch now takes the sum of the save times. Your team models how long a batch takes under a concurrency limit, to put a number on the regression and to size the fix.
An assistant wrote batch_wall_clock_ms(durations, max_concurrency), the model. It is in the editor. It has a bug. Find it and fix it.
Input and output:
durations: how long eachsavetakes, in ms, in the order the items arrive.max_concurrency: how many saves may run at the same time.- Return how long the whole batch takes, in ms.
Rules the tests check:
- If there are no items, return 0. Treat a
max_concurrencybelow 1 as 1. - Model a pool of
max_concurrencyworkers, numbered from 0, that all start at time 0. - Hand out the items in arrival order. Each item goes to the worker that becomes free first, and on a tie, to the lowest numbered one. A worker starts its next item the moment it finishes the previous one.
- The batch ends when the last worker finishes.
Examples
batch_wall_clock_ms(durations: list[int], max_concurrency: int) -> intExample 1
Input
batch_wall_clock_ms([100, 100, 100], 1)Output
300Example 2
Input
batch_wall_clock_ms([100, 100, 100], 3)Output
100Example 3
Input
batch_wall_clock_ms([100, 200, 50], 2)Output
200