FunctionRacerProblemsRace

Round Robin Completion Times

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

A single processor runs tasks round-robin with a time slice of quantum. Task i arrives at time arrival[i] and needs work[i] units of processor time. arrival is sorted from earliest to latest, and several tasks may arrive at the same time.

Return a list where item i is the time at which task i finishes its work.

Example 1

arrival [0,0,0]
work [3,1,2]
quantum 2
returns [6,3,5]

Task 0 runs 2 units (time 2) and rejoins behind tasks 1 and 2. Task 1 finishes at 3, task 2 at 5, and task 0 runs its last unit until 6.

Example 2

arrival [0,2]
work [4,2]
quantum 2
returns [6,4]

Task 0 runs until time 2. Task 1 arrives exactly then, so it joins the queue before task 0 rejoins: task 1 finishes at 4, then task 0 at 6.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems