Data Structures & AlgorithmsHardProduct Company SDE · scheduling OA

Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on or before their deadlines? Run the code.

Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on or before their deadlines? Run the code.
tasks = [(3, 5), (2, 4), (4, 9), (1, 3)]
tasks.sort(key=lambda t: t[1])
t = 0
ok = 0
for d, dl in tasks:
    t += d
    if t <= dl:
        ok += 1
print(ok)
What is printed?
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 1

Correct answer: B. 2

In deadline order the finish times are 1, 3, 6, 10 against deadlines 3, 4, 5, 9, so tasks with durations 1 and 2 meet their deadlines and the count is 2.

More Coding Interview Problems questions

Exams that ask this topic