Fit the most tasks into an hour across machine slots
I was asked this
No reports yet. Sign in to say where you were asked it.
Your team runs a batch cluster of n machines with IDs 0 to n-1. Each machine has m execution slots, so it can run up to m tasks at the same time. A slot runs one task at a time; when a task ends, the slot can start the next one immediately.
You get a list durations of task run times in whole minutes. A task must run on a single slot from start to finish: it cannot be split, paused, or moved. All slots are free at minute 0.
Implement:
def max_tasks(n: int, m: int, durations: list[int], limit: int = 60) -> int:
...
Return the largest number of tasks that can finish by minute limit (60 by default). You choose which tasks to run and where.
Constraints
-
0 <= n, m, andn * m <= 30 0 <= len(durations) <= 40-
1 <= durations[i] <= 10^4(a task longer thanlimitcan never finish)
Example 1
n = 1, m = 1, durations = [10, 20, 30, 40]
-> 3
One slot runs 10, 20 and 30 back to back, ending exactly at minute 60.
Example 2
n = 2, m = 1, durations = [3, 3, 4, 5, 5], limit = 10
-> 5
Machine 0 runs 5 then 5. Machine 1 runs 3, 3 and 4. Placing each shortest task on the least-busy machine finishes only 4, so the order of placement matters.
Be ready to explain whether your method is always optimal.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
