ORIGINAL CODING QUESTION
Shortest-first job dispatcher
A single build agent runs jobs one at a time without interruption. Whenever it is idle and at least one job has arrived and is waiting, it starts the waiting job with the shortest duration; ties go to the earlier arrival, then to the smaller job index. If nothing is waiting, it stays idle until the next arrival. A job arriving exactly when the agent frees up is already waiting. Return the sum of all job completion times.
Original exercise · hard. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n pairs (arrival,duration) in any order; job indexes follow input order.
Constraints
1 ≤ n ≤ 200; 0 ≤ arrival ≤ 1000; 1 ≤ duration ≤ 100.
Example 1
Input
3 0 8 1 4 2 1
Output
30
Job 0 runs from 0 to 8; then job 2 finishes at 9 and job 1 at 13, totalling 30.
Example 2
Input
2 5 3 0 2
Output
10
Job 1 finishes at 2, the agent idles until 5, and job 0 finishes at 8, totalling 10.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Process arrivals in time order, not input order.
Hint 2
Keep every arrived-but-waiting job in a min-heap keyed by (duration, arrival, index).
Hint 3
If the heap is empty, jump the clock to the next arrival before choosing.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →