Bird
Raised Fist0

What is the value of max_platforms returned for the input arrivals = [900, 940, 950] and departures = [910, 1200, 1120]?

easy🧾 Code Trace Q12 of Q15
Intervals - Minimum Number of Platforms Required
Consider the following code snippet implementing the sweep line algorithm to find the minimum number of platforms required. What is the value of max_platforms returned for the input arrivals = [900, 940, 950] and departures = [910, 1200, 1120]?
A4
B1
C3
D2
Step-by-Step Solution
Solution:
  1. Step 1: Create and sort events

    Events: (900,1), (940,1), (950,1), (910,-1), (1200,-1), (1120,-1). Sorted: (900,1), (910,-1), (940,1), (950,1), (1120,-1), (1200,-1).
  2. Step 2: Sweep through events updating platforms_needed and max_platforms

    At 900 arrival: platforms_needed=1, max=1; at 910 departure: platforms_needed=0; at 940 arrival: platforms_needed=1; at 950 arrival: platforms_needed=2, max=2; at 1120 departure: platforms_needed=1; at 1200 departure: platforms_needed=0.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Maximum concurrent trains is 2 [OK]
Quick Trick: Sort events and track increments/decrements [OK]
Common Mistakes:
MISTAKES
  • Misordering events when arrival and departure times are close
  • Off-by-one errors in counting platforms
  • Ignoring departure before arrival at same time
Trap Explanation:
PITFALL
  • Misordering events or ignoring departure before arrival at same time leads to overcounting platforms.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute sweep line code and handle event ordering.
Master "Minimum Number of Platforms Required" in Intervals

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 Intervals Quizzes