Drill

ProblemsTypeScript › machines

How much work is still queued

hardmachinesTypeScript

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

Solve it in the editor →

Where you start

function queueAfter(arrivals: number[], serviceRate: number): number {
  
}

Worked examples

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

Hint

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

Reference solution in TypeScript
function queueAfter(arrivals: number[], serviceRate: number): number {
  let pending = 0;
  for (const a of arrivals) {
    pending += a;
    pending -= Math.min(pending, serviceRate);
  }
  return pending;
}

The same problem in another language

More machines problems in TypeScript