What they’re really asking
If you only remember one thing, say this in the first minute: “First, which goal: the smallest total of finish times, or the earliest time the last task finishes? They sound alike and are different problems. For the total, shortest task first onto whichever machine frees up first is optimal, and I can prove it by swapping. For the last finish time, the problem is NP-hard, so I’d use longest task first as a heuristic and say it isn’t guaranteed optimal.”
The rest of this page explains each part of that answer.
You are given tasks with durations and a fixed number of machines (GPUs, workers, slots). You choose which machine runs each task and in what order. It looks like a standard greedy problem, and it is one, but only once you know what you are minimizing.
That is what makes this question different from most greedy screens. Two goals sound the same in plain English and have completely different answers:
- Total completion time: add up the time at which each task finishes. Dividing by the number of tasks gives the average wait, which is what users feel.
- Makespan: the time at which the last task finishes. This is what matters when you need the whole batch done.
One goal has a simple optimal algorithm. The other is NP-hard: no known algorithm solves every case quickly, so you use a good heuristic. Asking which one they mean is the most important minute of the round.
(This was reported by an NVIDIA Senior candidate, who “approached it like a standard LeetCode scheduling problem”, focused on “getting to a correct implementation quickly” Aced. The report doesn’t give the exact objective.)
Words you need first
- SPT (shortest processing time first): sort tasks by duration, shortest first.
- LPT (longest processing time first): sort longest first, and give each task to the least-loaded machine.
- Exchange argument: a proof that shows any schedule breaking your rule can be improved by swapping two tasks, so only your rule’s schedule can be optimal.
- Heap: a priority queue that always hands back the smallest item; here, the machine that frees up first.
Clarify first
- Objective: the sum of completion times (average wait), or the makespan (when the last task finishes)?
- Resources: m identical machines, or resources with capacities (GPUs, memory) per task?
- Dependencies between tasks? Release times (tasks that arrive later)?
- Preemption: may a task be paused and resumed?
- Output: the total, or the full plan (task, machine, start, end)?
Say it like this: “Before I write anything: total completion time or the time the last task finishes? They are different problems.”
Answer
The example to follow
Five jobs on 2 GPUs, in the order they were submitted: 6, 1, 4, 2 and 3 hours.
Run them in submission order (first-in-first-out), each on the GPU that frees up first. The jobs finish at 6, 1, 5, 7 and 9 hours. The total is 28 hours.
Run them shortest first (SPT):
| Job (hours) | GPU | Starts | Ends | What to say in the interview |
|---|---|---|---|---|
| 1 | GPU 0 | 0 | 1 | ”Shortest first, onto the first free GPU.” |
| 2 | GPU 1 | 0 | 2 | ”Both GPUs start immediately.” |
| 3 | GPU 0 | 1 | 4 | ”GPU 0 frees up first, at hour 1.” |
| 4 | GPU 1 | 2 | 6 | |
| 6 | GPU 0 | 4 | 10 | ”The longest job goes last, so it delays nobody.” |
The finish times are 1, 2, 4, 6 and 10, for a total of 23 hours. A brute force over every assignment also gives 23, so SPT is optimal here. (These numbers come from running the code below on 2026-10-12.)
The code
For the sum of completion times on m identical machines, no dependencies and no release times: run the shortest task first, each on whichever machine frees up first.
def spt_schedule(durations, m):
free = [(0, i) for i in range(m)] # (time the machine frees up, machine)
total, plan = 0, []
for job, d in sorted(enumerate(durations), key=lambda x: x[1]):
t, machine = heapq.heappop(free)
total += t + d
plan.append((job, machine, t, t + d))
heapq.heappush(free, (t + d, machine))
return total, plan
The heap holds one entry per machine: the time it frees up. Each task pops the earliest-free machine, runs there, and pushes the machine back with its new free time. Cost: O(n log n) for the sort, plus O(log m) per task for the heap.
Why it is optimal
Say it in one breath. On one machine, if a longer task runs right before a shorter one, swapping them makes the shorter one finish earlier by the longer one’s length and the longer one later by the shorter one’s, so the sum drops. Sorted order is the only schedule with no improving swap. Dealing sorted tasks to the next free machine extends this to m machines.
In tests run for this page on 2026-10-12, SPT matched a brute force over every assignment on 300 random instances (up to 6 tasks, 3 machines). Write a brute force like that in the interview if you have five minutes; it is the strongest correctness argument available.
The arithmetic behind the numbers (for a Staff-level follow-up)
- Swap on one machine: tasks a (long) then b (short), starting at time t. Before: a ends at t + a, b at t + a + b. After the swap: b ends at t + b, a at t + a + b. The sum changes by (t + b) − (t + a) = b − a, which is negative when b < a.
- First-in-first-out example: GPU 0 runs 6 (ends 6) then 3 (ends 9); GPU 1 runs 1, 4, 2 (ends 1, 5, 7). 6 + 1 + 5 + 7 + 9 = 28.
- SPT example: 1 + 2 + 4 + 6 + 10 = 23.
- Makespan of the same jobs: SPT finishes last at hour 10. LPT gives 6 + 2 = 8 on one GPU and 4 + 3 + 1 = 8 on the other. The jobs add up to 16 hours, so 2 GPUs can’t finish before 16 ÷ 2 = 8; LPT’s 8 is the best possible here, which a brute force also confirms.
If the goal is the last finish time
If the objective is makespan, the problem is NP-hard for two or more machines. Say so, then offer longest-processing-time first onto the least-loaded machine as a heuristic, and an exact search for small n.
The example shows why the goal matters. SPT, which was optimal for the total, finishes the last job at hour 10. LPT puts 6 + 2 on one GPU and 4 + 3 + 1 on the other, so both finish at hour 8. Same jobs, same GPUs, different best answer.
Other variants to recognize
If there are dependencies, schedule in topological order (an order where each task comes after the tasks it depends on), picking among ready tasks by a rule (shortest first, or longest remaining path first); say it is a heuristic. If tasks have cooldowns (a LeetCode “task scheduler” variant), the answer is a counting formula, not a heap; ask which variant they mean.
Senior vs Staff
Scheduling on limited resources
Senior
- Pins down the objective, implements SPT with a heap, explains the exchange argument, tests it.
Staff
- Names which variants are NP-hard, offers heuristics with their trade-offs, and connects the objective to the product (average latency versus job completion).
Small-team version
The objective question is the useful part at work. When a small team queues batch jobs on a few GPUs, shortest-first minimizes average wait but can starve a long fine-tuning job: short jobs keep jumping ahead and the long one never starts. First-in-first-out is fairer and simpler.
What to do: use your queue’s default plus priorities. That is the right call until waits are measured and someone is unhappy.
Order changes the wait, not the bill. Jobs of 10, 1 and 1 hours on one GPU finish at 10, 11 and 12 hours first-in-first-out (mean 11 h) and at 1, 2 and 12 hours shortest-first (mean 5 h). Both use 12 GPU-hours, $24 at an assumed $2 per GPU-hour.
Pitfalls
- Coding before asking for the objective. You may solve the wrong problem perfectly.
- Claiming SPT minimizes makespan. It doesn’t; in the example it finishes at 10 against a best of 8.
- Claiming any greedy is optimal once dependencies exist. With dependencies, greedy rules are heuristics.
- Sorting per machine. Split tasks across machines first and you lose optimality; deal globally sorted tasks to the next free machine instead.
Go deeper
- Family walkthrough: Short DSA screens with an infrastructure flavor, “What to say out loud on each”.
- Same candidate’s other onsite round: padding ragged arrays.