ProgrammingHardTCS NQT · Accenture

Tasks (duration, deadline): (3,5), (2,4), (4,9), (1,3). Earliest deadline first is applied: tasks are sorted by deadline and run back to back from time 0. Lateness of a task is finish time minus deadline. What is printed as the maximum lateness?

Tasks (duration, deadline): (3,5), (2,4), (4,9), (1,3). Earliest deadline first is applied: tasks are sorted by deadline and run back to back from time 0. Lateness of a task is finish time minus deadline. What is printed as the maximum lateness?
sort tasks by deadline ascending
time = 0; best = negative infinity
for each (d, dl) in sorted order
    time = time + d
    best = max(best, time - dl)
end for
Print best
  1. A. 7
  2. B. 2
  3. C. 20
  4. D. 1

Correct answer: D. 1

Sorted by deadline, the finish times are 1, 3, 6 and 10 against deadlines 3, 4, 5 and 9, so the largest lateness is 1 (the third task). Input order gives 7, counting late tasks gives 2, and summing the finish times gives 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic