Data Structures & AlgorithmsVery hardProduct company OA

Problem: Single machine, jobs (name, duration, due time); lateness = finish − due. Minimise the MAXIMUM lateness. Compare earliest-due-date (EDD) order, shortest-job-first and the given order.

Problem: Single machine, jobs (name, duration, due time); lateness = finish − due. Minimise the MAXIMUM lateness. Compare earliest-due-date (EDD) order, shortest-job-first and the given order.
jobs = [("A", 4, 9), ("B", 2, 4), ("C", 3, 12), ("D", 1, 3), ("E", 5, 15)]
def max_late(order):
    t = worst = 0
    for name, dur, due in order:
        t += dur; worst = max(worst, t - due)
    return worst
edd = sorted(jobs, key=lambda j: j[2])
spt = sorted(jobs, key=lambda j: j[1])
print("".join(j[0] for j in edd), max_late(edd), max_late(spt), max_late(jobs))
What is printed?
  1. A. DBACE 1 0 7
  2. B. BDACE 0 1 7
  3. C. DBACE 0 1 7
  4. D. DBACE 0 3 5

Correct answer: C. DBACE 0 1 7

EDD is optimal for maximum lateness by an exchange argument: swapping an adjacent inverted pair never increases the worse of the two latenesses. EDD order D(1, 3), B(2, 4), A(4, 9), C(3, 12), E(5, 15) finishes at 1, 3, 7, 10, 15 → all on time, max lateness 0. Shortest job first gives D, B, C, A, E: A finishes at 10 → lateness 1. The given order is 7 late. SJF is optimal for total completion time, not for lateness.

More Coding Interview Problems questions

Exams that ask this topic