What is the problem statement of the Minimum Number of Platforms?
Given arrival and departure times of trains, find the minimum number of platforms required so that no train waits.
Click to reveal answer
beginner
Why do we sort arrival and departure times separately in the Minimum Number of Platforms problem?
Sorting helps to process events in chronological order, making it easier to track how many trains are at the station at the same time.
Click to reveal answer
beginner
What does the variable 'platforms_needed' represent during the algorithm?
'platforms_needed' tracks the current number of trains at the station, indicating how many platforms are occupied at that moment.
Click to reveal answer
beginner
How do we update the count of platforms when a train arrives or departs?
When a train arrives, increment 'platforms_needed'. When a train departs, decrement 'platforms_needed'.
Click to reveal answer
beginner
What is the final answer in the Minimum Number of Platforms problem?
The maximum value of 'platforms_needed' during the process is the minimum number of platforms required.
Click to reveal answer
Why do we need to sort arrival and departure times separately?
ATo find the longest train
BTo group trains by their platform
CTo process events in chronological order
DTo count total trains
✗ Incorrect
Sorting arrival and departure times separately allows us to process train events in order of time.
What does an increase in 'platforms_needed' signify?
AA train has departed
BNo change
CA platform is freed
DA train has arrived
✗ Incorrect
When a train arrives, 'platforms_needed' increases because one more platform is occupied.
If arrival[i] > departure[j], what should we do in the algorithm?
AIncrement platforms_needed
BDecrement platforms_needed
CDo nothing
DReset platforms_needed
✗ Incorrect
If a train departs before the next arrives, we free a platform by decrementing 'platforms_needed'.
What is the time complexity of the Minimum Number of Platforms algorithm after sorting?
AO(n log n)
BO(n^2)
CO(n)
DO(log n)
✗ Incorrect
Sorting takes O(n log n), and the traversal is O(n), so overall O(n log n).
What does the maximum value of 'platforms_needed' represent?
AMinimum platforms required
BTotal trains
CMaximum waiting time
DNumber of trains departed
✗ Incorrect
The maximum 'platforms_needed' is the minimum number of platforms needed to avoid waiting.
Explain the step-by-step approach to find the minimum number of platforms needed for trains.
Think about processing train events in order of time.
You got /6 concepts.
Why is sorting both arrival and departure times separately important in this problem?
Consider how to know when a platform frees up before next train arrives.
You got /3 concepts.
Practice
(1/5)
1. What does the "Minimum Number of Platforms" problem ask you to find given train arrival and departure times?
easy
A. The total time trains spend on the platform
B. The total number of trains arriving in a day
C. The maximum speed of trains on the platform
D. The least number of platforms needed so no train waits
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of platforms required so that no train has to wait for another to leave.
Step 2: Clarify what is being counted
We count overlapping train schedules to find the peak number of trains at the station simultaneously.
Final Answer:
The least number of platforms needed so no train waits -> Option D
Quick Check:
Minimum platforms = max trains overlapping [OK]
Hint: Count overlapping trains to find minimum platforms needed [OK]
Common Mistakes:
Confusing total trains with platforms needed
Ignoring overlapping times
Counting total arrival or departure times only
2. Which of the following TypeScript code snippets correctly sorts an array of train arrival times stored as numbers?
easy
A. arrivals.sort();
B. arrivals.sort((a, b) => a - b);
C. arrivals.sort((a, b) => b - a);
D. arrivals.sort((a, b) => a + b);
Solution
Step 1: Understand sorting numbers in TypeScript
Default sort() treats elements as strings, so numeric sort needs a compare function.
Step 2: Identify correct compare function
The function (a, b) => a - b sorts numbers in ascending order correctly.
Final Answer:
arrivals.sort((a, b) => a - b); -> Option B
Quick Check:
Numeric ascending sort uses (a, b) => a - b [OK]
Hint: Use (a, b) => a - b to sort numbers ascending [OK]
Common Mistakes:
Using sort() without compare function for numbers
Using (a, b) => a + b which is invalid
Sorting descending when ascending needed
3. Given arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000], what is the minimum number of platforms needed?
Hint: Track arrivals and departures to find max overlap [OK]
Common Mistakes:
Not sorting times before processing
Counting total trains instead of overlaps
Mixing arrival and departure times
4. The following TypeScript code tries to find the minimum number of platforms but has a bug. What is the bug?
function findPlatforms(arrivals: number[], departures: number[]): number {
arrivals.sort((a, b) => a - b);
departures.sort((a, b) => a - b);
let platformNeeded = 1, maxPlatforms = 1;
let i = 1, j = 0;
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] <= departures[j]) {
platformNeeded++;
i++;
} else {
platformNeeded--;
j++;
}
if (platformNeeded > maxPlatforms) maxPlatforms = platformNeeded;
}
return maxPlatforms;
}
medium
A. Start i from 0 instead of 1 to include first arrival
B. Use < instead of <= in comparison to avoid counting overlaps wrongly
C. Initialize platformNeeded to 0 instead of 1
D. Sort arrivals and departures in descending order
Solution
Step 1: Analyze loop initialization
Variable i starts at 1, skipping the first arrival time, which causes missing counting the first train.
Step 2: Correct initialization for accurate counting
Starting i at 0 ensures all arrivals are considered, giving correct platform count.
Final Answer:
Start i from 0 instead of 1 to include first arrival -> Option A
Quick Check:
Include all arrivals by starting i=0 [OK]
Hint: Start pointers at 0 to include all trains [OK]
Common Mistakes:
Skipping first arrival by starting i at 1
Changing comparison operator incorrectly
Initializing platformNeeded wrongly
5. You have trains arriving and departing as follows:
Arrivals: [1000, 1010, 1025, 1030, 1040]
Departures: [1015, 1020, 1035, 1045, 1050]
If you want to minimize the number of platforms by rescheduling trains without changing their arrival order, which train's departure time should you delay to reduce platform count?
hard
A. Delay the departure of train arriving at 1010
B. Delay the departure of train arriving at 1000
C. Delay the departure of train arriving at 1040
D. Delay the departure of train arriving at 1025
Solution
Step 1: Identify overlapping trains causing max platforms
Trains at 1010 and 1025 overlap because 1010 departs at 1020 and 1025 arrives before that.
Step 2: Delay departure of train at 1010 to after 1025 departs
Delaying 1010's departure reduces overlap with 1025, lowering platform need.
Final Answer:
Delay the departure of train arriving at 1010 -> Option A
Quick Check:
Delay overlapping train's departure to reduce platforms [OK]
Hint: Delay departure of earliest overlapping train to reduce platforms [OK]
Common Mistakes:
Delaying wrong train's departure
Changing arrival order which is fixed
Ignoring overlap timing details
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.