ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Pair iv[11] = {(1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16)}
sort iv by end time (ties keep the given order)
Integer lastEnd = -1, chosen = 0
for (each (s, f) in iv)
    if (s >= lastEnd)
        chosen = chosen + 1
        lastEnd = f
    end if
end for
Print chosen, lastEnd
  1. A. 3 16
  2. B. 5 16
  3. C. 4 14
  4. D. 4 16

Correct answer: D. 4 16

Greedy activity selection by earliest finish: (1, 4), then (5, 7), then (8, 11), then (12, 16). chosen = 4 and the last end time is 16.

More Pseudocode and Code Tracing questions

Exams that ask this topic