Finish times for round-robin tasks
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Microsoft. It tests simulation, sorting and fenwick tree.
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^51 <= 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.
Clarifying questions
- Is an empty list valid, and should it return
[]? - Are the tasks always started in index order, with no new tasks arriving later?
- Is the answer for each task the hour it finishes (not the time spent waiting)? The function name says waiting time.
- Can values be large enough that the answers need 64-bit integers?
What a strong answer covers
- Sees that by the time task
ifinishes, every other taskjhas runmin(arr[j], arr[i] - 1)hours in the earlier cycles, plus one more hour in the last cycle ifj <= iandarr[j] >= arr[i]. - Sorts the values (or the tasks by value) and uses prefix sums, so
sum(min(arr[j], a - 1))comes from one binary search and one prefix lookup. That is O(n log n). - Counts the tasks with
arr[j] >= arr[i]andj <= iwith a pass over tasks grouped by value in index order, or with a Fenwick tree. - Explains why simulation is too slow: the total hours can be about
10^14. - Tests: both examples, a single task, equal values, strictly increasing and decreasing values, and a check against an hour-by-hour simulation on small inputs.
Follow-ups
- Each turn runs for
qhours instead of one (a time quantum). - Tasks arrive at different times.
- Return the order in which tasks finish.
- There are several workers taking from the same queue.
Where it's asked
What it tests
simulation · sorting · fenwick tree · queue
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
