Drill

ProblemsJava › machines

How much work is still queued

hardmachinesArraysSimulationJava

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.

queueAfter(arrivals: list<int>, serviceRate: int) → int

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int queueAfter(List<Integer> arrivals, int serviceRate) {
    
}

Worked examples

CallResult
queueAfter(Main.<Integer>ls(5, 3, 2), 4)0
queueAfter(Main.<Integer>ls(10, 0, 5), 4)3
queueAfter(Main.<Integer>ls(3, 3, 3), 1)6
queueAfter(Main.<Integer>ls(10, 10), 0)20

Hint

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

Reference solution in Java
int queueAfter(List<Integer> arrivals, int serviceRate) {
    int pending = 0;
    for (int a : arrivals) {
        pending += a;
        pending -= Math.min(pending, serviceRate);
    }
    return pending;
}

The same problem in another language

More machines problems in Java