← Questions

Finish times for round-robin tasks

0
MicrosoftCoding

A single worker processes a list of tasks. Task i needs arr[i] hours of work in total.

The tasks wait in a queue in index order. Each turn, the worker takes the task at the front and works on it for exactly one hour. If the task still needs more time, it goes to the back of the queue. Otherwise it is finished and leaves. The worker never idles while a task is unfinished.

Implement:

total_waiting_time(arr: list[int]) -> list[int]

Return a list finish where finish[i] is the number of hours from the start until task i is finished. This count includes the hours the task itself ran.

Example

arr = [2, 1, 3]

Hour Runs Note
1 task 0 needs 1 more, goes to the back
2 task 1 finished
3 task 2 needs 2 more
4 task 0 finished
5 task 2 needs 1 more
6 task 2 finished

Output: [4, 2, 6].

A second example: arr = [3, 3, 3] gives [7, 8, 9]. Tied tasks finish in index order during the last cycle.

Constraints

  • 0 <= n <= 10^5
  • 1 <= arr[i] <= 10^9
  • A direct hour-by-hour simulation is too slow for these limits. Aim for O(n log n).
  • Answers can exceed 2^31.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding