Drill

ProblemsPython › machines

How much work is still queued

hardmachinesPython

Jobs arrive in batches and a single machine clears a fixed number per tick. Work that is not cleared this tick carries into the next.

queue_after(arrivals: list<int>, service_rate: int) → int

Solve it in the editor →

Where you start

def queue_after(arrivals: list[int], service_rate: int) -> int:
    

Worked examples

CallResult
queue_after([5, 3, 2], 4)0
queue_after([10, 0, 5], 4)3
queue_after([3, 3, 3], 1)6
queue_after([10, 10], 0)20

Hint

For each arrival add it, then subtract the smaller of the queue and the rate.

Reference solution in Python
def queue_after(arrivals: list[int], service_rate: int) -> int:
    pending = 0
    for a in arrivals:
        pending += a
        pending -= min(pending, service_rate)
    return pending

The same problem in another language

More machines problems in Python