Bird
Raised Fist0

Identify the logical error: ```python def min_platforms(arrivals, departures): arrivals.sort() departures.sort() i = j = 0 platforms = max_platforms = 0 while i < len(arrivals) and j < len(departures): if arrivals[i] <= departures[j]: platforms += 1 i += 1 else: platforms -= 1 j += 1 max_platforms = max(max_platforms, platforms) return max_platforms ```

medium🐞 Bug Identification Q7 of Q15
Intervals - Minimum Number of Platforms Required
Review the following Python code snippet for calculating minimum platforms. Identify the logical error: ```python def min_platforms(arrivals, departures): arrivals.sort() departures.sort() i = j = 0 platforms = max_platforms = 0 while i < len(arrivals) and j < len(departures): if arrivals[i] <= departures[j]: platforms += 1 i += 1 else: platforms -= 1 j += 1 max_platforms = max(max_platforms, platforms) return max_platforms ```
AThe condition should be arrivals[i] < departures[j] to avoid counting simultaneous events incorrectly
BThe code does not sort the input arrays
CThe pointers i and j are incremented incorrectly
DThe max_platforms variable is not updated inside the loop
Step-by-Step Solution
Solution:
  1. Step 1: Understand event ordering

    When arrival and departure times are equal, the platform can be freed before the next train arrives.
  2. Step 2: Correct comparison operator

    Using < instead of <= ensures departures at the same time free platforms before arrivals.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Check equality case carefully to avoid overcounting [OK]
Quick Trick: Use strict less than to handle simultaneous arrival and departure [OK]
Common Mistakes:
MISTAKES
  • Using <= causes overcounting platforms when times are equal
  • Ignoring sorting step (though code sorts here)
  • Not updating max_platforms inside loop
Trap Explanation:
PITFALL
  • Using <= seems intuitive but causes counting errors for simultaneous events.
Interviewer Note:
CONTEXT
  • Tests attention to detail in event ordering and boundary conditions.
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