ProgrammingVery hardTCS NQT · Accenture

Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The procedure below schedules as many jobs as possible, using each slot at most once, by taking jobs in deadline order and placing each in the latest free slot at or before its deadline. What is the number of jobs scheduled?

Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The procedure below schedules as many jobs as possible, using each slot at most once, by taking jobs in deadline order and placing each in the latest free slot at or before its deadline. What is the number of jobs scheduled?
sort deadlines ascending
for each deadline d
    find the latest free slot t with 1 <= t <= d; if found, occupy it and count + 1
Print count
  1. A. 4
  2. B. 6
  3. C. 1
  4. D. 5

Correct answer: A. 4

Only four slots are useful for these deadlines, so at most four jobs can run, and the greedy reaches that limit. Counting every job ignores the slot limit, stopping at the first conflict gives a smaller number, and adding the tight-deadline count by mistake gives 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic