Bird
Raised Fist0

Identify the line containing the subtle bug that can cause incorrect platform count when a train departs and another arrives at the same time.

medium🐞 Bug Identification Q14 of Q15
Intervals - Minimum Number of Platforms Required
Examine the following code snippet intended to compute the minimum number of platforms required. Identify the line containing the subtle bug that can cause incorrect platform count when a train departs and another arrives at the same time.
ALine 6: Appending departure events with -1 instead of 1
BLine 10: Not resetting platforms_needed before loop
CLine 8: Sorting events with arrival before departure on tie
DLine 12: Not updating max_platforms after each event
Step-by-Step Solution
Solution:
  1. Step 1: Understand event sorting importance

    When arrival and departure times are equal, departures must be processed before arrivals to free platforms.
  2. Step 2: Identify sorting key bug

    The code sorts with arrival (1) before departure (-1) on tie, causing overcounting platforms.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Departure before arrival on tie avoids overcounting [OK]
Quick Trick: Departure events must come before arrival on tie [OK]
Common Mistakes:
MISTAKES
  • Sorting arrival before departure on tie
  • Forgetting to update max_platforms
  • Incorrect event type values
Trap Explanation:
PITFALL
  • Sorting arrival before departure on tie looks intuitive but causes platform overcounting.
Interviewer Note:
CONTEXT
  • Tests attention to subtle event ordering bugs that cause wrong answers on edge cases.
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