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?- A. DBACE 1 0 7
- B. BDACE 0 1 7
- C. DBACE 0 1 7
- 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
- Problem: Two sorted price lists; list the 6 smallest sums a[i] + b[j] without generating all pairs (heap of frontier cells with a…
- Problem: Find all critical links (bridges) and critical routers (articulation points) with one DFS using discovery times and low-links…
- Problem: A drone must fly every route leg exactly once starting at P, choosing the alphabetically smallest destination whenever possible…
- Problem: Assign interns to projects; intern u may take only projects in adj[u], each project at most one intern. Maximum bipartite…
- Problem: For every warehouse in a tree network, compute the sum of distances to all other warehouses with two passes (rerooting)…
- Problem: Count integers in [1, N] divisible by at least one of the given numbers using inclusion–exclusion with LCMs (N up to 10⁹, so no…