How much work is still queued
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.
- Each arrival adds its batch to the pending queue.
- Then the machine clears serviceRate of it — never more than what is pending.
- Each arrival is processed in sequence; return the queue left after the last tick.
- A service rate of zero or less clears nothing, so the sum of all arrivals is left.
queueAfter(arrivals: list<int>, serviceRate: int) → int
Go 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.
Where you start
func queueAfter(arrivals []int, serviceRate int) int {
}
Worked examples
| Call | Result |
|---|---|
queueAfter([]int{5, 3, 2}, 4) | 0 |
queueAfter([]int{10, 0, 5}, 4) | 3 |
queueAfter([]int{3, 3, 3}, 1) | 6 |
queueAfter([]int{10, 10}, 0) | 20 |
Hint
For each arrival add it, then subtract the smaller of the queue and the rate.
Reference solution in Go
func queueAfter(arrivals []int, serviceRate int) int {
pending := 0
for _, a := range arrivals {
pending += a
if serviceRate < pending {
pending -= serviceRate
} else {
pending = 0
}
}
return pending
}