ProgrammingHardTCS NQT · Accenture

Train (arrival, departure) times in HHMM form: arrivals 900, 940, 950, 1100, 1500, 1800 and departures 910, 1200, 1120, 1130, 1900, 2000. Each train needs a platform from arrival to departure. The procedure below sorts both lists and runs. What is printed?

Train (arrival, departure) times in HHMM form: arrivals 900, 940, 950, 1100, 1500, 1800 and departures 910, 1200, 1120, 1130, 1900, 2000. Each train needs a platform from arrival to departure. The procedure below sorts both lists and runs. What is printed?
i = 0; j = 0; plat = 0; best = 0
while i < number of trains
    if arrival[i] <= departure[j] then
        plat = plat + 1; i = i + 1
    else
        plat = plat - 1; j = j + 1
    end if
    best = max(best, plat)
end while
Print best
  1. A. 6
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: C. 3

At 950 three trains are present, so the peak is 3 platforms. Printing the train count, the final plat value, or counting only the arrivals before 1200 (ignoring departures) each gives a different number.

More Pseudocode and Code Tracing questions

Exams that ask this topic