Challenge - 5 Problems
Platform Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateMinimum Platforms Needed for Given Train Schedule
What is the output of the following code that calculates the minimum number of platforms needed for trains arriving and departing at given times?
DSA Python
def find_min_platforms(arrivals, departures): arrivals.sort() departures.sort() platform_needed = 0 max_platforms = 0 i = 0 j = 0 n = len(arrivals) while i < n and j < n: if arrivals[i] < departures[j]: platform_needed += 1 max_platforms = max(max_platforms, platform_needed) i += 1 else: platform_needed -= 1 j += 1 return max_platforms arrivals = [900, 940, 950, 1100, 1500, 1800] departures = [910, 1200, 1120, 1130, 1900, 2000] print(find_min_platforms(arrivals, departures))
Attempts:
2 left
💡 Hint
Sort arrival and departure times, then count platforms needed by comparing arrivals and departures.
✗ Incorrect
The code sorts arrival and departure times. It uses two pointers to track trains arriving and leaving. When a train arrives before the earliest departure, it increases platform count. When a train departs, it decreases platform count. The maximum platforms needed at any time is 3.
🧠 Conceptual
intermediateUnderstanding Platform Calculation Logic
In the minimum number of platforms problem, why do we sort both arrival and departure times before processing?
Attempts:
2 left
💡 Hint
Think about how sorting helps in comparing times step-by-step.
✗ Incorrect
Sorting arrivals and departures allows us to process train events in time order. This helps to count how many trains are at the station simultaneously, which determines the minimum platforms needed.
❓ Predict Output
advancedOutput of Platform Calculation with Overlapping Times
What is the output of this code snippet calculating minimum platforms for overlapping train times?
DSA Python
arrivals = [1000, 1010, 1025, 1030] departures = [1020, 1035, 1040, 1050] def min_platforms(arr, dep): arr.sort() dep.sort() i = j = 0 count = max_count = 0 n = len(arr) while i < n and j < n: if arr[i] < dep[j]: count += 1 max_count = max(max_count, count) i += 1 else: count -= 1 j += 1 return max_count print(min_platforms(arrivals, departures))
Attempts:
2 left
💡 Hint
Check how many trains overlap at the same time.
✗ Incorrect
The trains arriving at 1000, 1010, and 1025 overlap before the first departure at 1020. So, maximum platforms needed is 3.
🔧 Debug
advancedIdentify the Error in Platform Calculation Code
What error does this code produce when calculating minimum platforms?
DSA Python
def min_platforms(arrivals, departures): arrivals.sort() departures.sort() i = j = 0 count = max_count = 0 n = len(arrivals) while i < n and j < n: if arrivals[i] < departures[j]: count += 1 max_count = max(max_count, count) i += 1 else: count -= 1 j += 1 return max_count arrivals = [900, 940, 950] departures = [910, 1200, 1120] print(min_platforms(arrivals, departures))
Attempts:
2 left
💡 Hint
Check loop conditions and index usage carefully.
✗ Incorrect
The loop condition uses 'i <= n' which allows i to become equal to n, causing an index out of range error when accessing arrivals[i].
🚀 Application
expertCalculate Minimum Platforms for Large Schedule
Given the following arrival and departure times, what is the minimum number of platforms needed?
DSA Python
arrivals = [900, 940, 950, 1100, 1500, 1800, 1830, 1845, 1900, 1930] departures = [910, 1200, 1120, 1130, 1900, 2000, 1850, 1905, 1950, 2000] def min_platforms(arr, dep): arr.sort() dep.sort() i = j = 0 count = max_count = 0 n = len(arr) while i < n and j < n: if arr[i] < dep[j]: count += 1 max_count = max(max_count, count) i += 1 else: count -= 1 j += 1 return max_count print(min_platforms(arrivals, departures))
Attempts:
2 left
💡 Hint
Sort and count overlapping trains carefully.
✗ Incorrect
The maximum overlap occurs around times 1830 to 1850 where 4 trains are at the station simultaneously, so minimum platforms needed is 4.
