Bird
Raised Fist0

Given the following code snippet implementing the min-heap approach for minimum platforms, what is the output for arrivals = [900, 940, 950, 1100, 1500, 1800] and departures = [910, 1200, 1120, 1130, 1900, 2000]?

easy🧾 Code Trace Q3 of Q15
Greedy Algorithms - Minimum Platforms (Train Stations)
Given the following code snippet implementing the min-heap approach for minimum platforms, what is the output for arrivals = [900, 940, 950, 1100, 1500, 1800] and departures = [910, 1200, 1120, 1130, 1900, 2000]?
A3
B2
C4
D5
Step-by-Step Solution
Solution:
  1. Step 1: Trace heap operations for each train

    Trains sorted by arrival: (900,910), (940,1200), (950,1120), (1100,1130), (1500,1900), (1800,2000). At 900, heap=[910]. At 940, heap=[910,1200]. At 950, heap=[910,1120,1200]. At 1100, pop 910 (<=1100), heap=[1120,1200], push 1130 -> heap size 3. Max platforms = 3.
  2. Step 2: Confirm max heap size is 3

    Later arrivals reduce heap size, max remains 3.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Heap size peaks at 3 platforms [OK]
Quick Trick: Heap size tracks active trains -> max platforms [OK]
Common Mistakes:
MISTAKES
  • Forgetting to pop from heap when train departs
  • Miscounting max heap size
  • Mixing arrival and departure times
Trap Explanation:
PITFALL
  • Candidates often forget to pop all trains that have departed before current arrival, inflating platform count.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute heap-based greedy code.
Master "Minimum Platforms (Train Stations)" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes