📖 Scenario: Imagine a small train station where trains arrive and depart at different times. We want to find out the minimum number of platforms needed so that no train has to wait.
🎯 Goal: You will write a program to find the minimum number of platforms required for the trains based on their arrival and departure times.
📋 What You'll Learn
Create two arrays called arrivals and departures with exact train times
Create a variable called platforms_needed to count platforms
Write a loop to find the minimum number of platforms needed
Print the final number of platforms needed
💡 Why This Matters
🌍 Real World
Train stations and airports use this logic to manage platforms or gates so that vehicles do not wait.
💼 Career
Understanding scheduling and resource allocation is important in software engineering, especially in systems design and operations.
Progress0 / 4 steps
1
Create train arrival and departure arrays
Create two arrays called arrivals and departures with these exact values: arrivals = [900, 940, 950, 1100, 1500, 1800] and departures = [910, 1200, 1120, 1130, 1900, 2000].
DSA Javascript
Hint
Use const to create two arrays named arrivals and departures with the exact times given.
2
Initialize platform counters
Create two variables called platforms_needed and max_platforms and set both to 1. Also create two variables i and j and set both to 1 and 0 respectively.
DSA Javascript
Hint
Initialize platforms_needed and max_platforms to 1, and pointers i and j to 1 and 0.
3
Calculate minimum platforms needed
Sort the arrivals and departures arrays. Then use a while loop with condition i < arrivals.length && j < departures.length. Inside the loop, if arrivals[i] <= departures[j], increment platforms_needed and i. Else, decrement platforms_needed and increment j. Update max_platforms to the maximum of itself and platforms_needed inside the loop.
DSA Javascript
Hint
Sort both arrays first. Then use a while loop to compare arrival and departure times. Increase or decrease platforms_needed accordingly and update max_platforms.
4
Print the minimum number of platforms needed
Write console.log(max_platforms) to print the minimum number of platforms needed.
DSA Javascript
Hint
Use console.log(max_platforms) to show the result.
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 least number of platforms needed so no train waits
B. The total number of trains arriving in a day
C. The longest time a train stays on a platform
D. The earliest train arrival time
Solution
Step 1: Understand the problem goal
The problem asks for the minimum platforms so that no train waits due to platform unavailability.
Step 2: Clarify what is being counted
We count platforms needed simultaneously for overlapping train times, not total trains or times.
Final Answer:
The least number of platforms needed so no train waits -> Option A
Quick Check:
Minimum platforms = least needed to avoid waiting [OK]
Hint: Count overlapping trains to find minimum platforms needed [OK]
Common Mistakes:
Confusing total trains with platforms needed
Thinking about train durations instead of overlaps
Ignoring simultaneous arrivals and departures
2. Which of the following JavaScript 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 JavaScript sort behavior
By default, sort() converts elements to strings and sorts lexicographically, which is incorrect for numbers.
Step 2: Use numeric comparison function
Using (a, b) => a - b sorts numbers in ascending order correctly.
Final Answer:
arrivals.sort((a, b) => a - b) -> Option B
Quick Check:
Numeric sort uses (a, b) => a - b [OK]
Hint: Use numeric compare (a, b) => a - b to sort numbers [OK]
Common Mistakes:
Using sort() without compare function for numbers
Using incorrect compare functions like a + b
Sorting in descending order when ascending needed
3. What is the minimum number of platforms needed for trains with arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]?
Step 2: Count platforms needed by comparing arrivals and departures
At 900 arrival, platform count=1; 940 arrival before 910 departure? No, so platform count=1; 950 arrival before 1120 departure? Yes, platform count=2; 1100 arrival before 1120 departure? Yes, platform count=3; Then departures reduce count as trains leave.
Final Answer:
3 -> Option D
Quick Check:
Max overlapping trains = 3 platforms needed [OK]
Hint: Count overlaps by comparing sorted arrivals and departures [OK]
Common Mistakes:
Not sorting times before counting
Counting total trains instead of overlaps
Mixing arrival and departure comparisons
4. Identify the error in this JavaScript snippet for calculating minimum platforms:
let i = 0, j = 0, platforms = 0, maxPlatforms = 0;
arrivals.sort();
departures.sort();
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] <= departures[j]) {
platforms++;
i++;
if (platforms > maxPlatforms) maxPlatforms = platforms;
} else {
platforms--;
j++;
}
}
return maxPlatforms;
medium
A. Using <= instead of < in if condition
B. Incrementing i and j in wrong order
C. Sorting arrays without numeric compare function
D. Not initializing maxPlatforms to arrivals.length
Solution
Step 1: Check sorting method
arrivals.sort() and departures.sort() sort lexicographically by default, causing wrong order for numbers.
Step 2: Correct sorting for numeric arrays
Use arrivals.sort((a,b) => a - b) and departures.sort((a,b) => a - b) to sort numbers properly.
Final Answer:
Sorting arrays without numeric compare function -> Option C
Quick Check:
Numeric sort needs compare function [OK]
Hint: Always use numeric compare in sort for numbers [OK]
Common Mistakes:
Sorting numbers as strings causing wrong order
Confusing comparison operators in if condition
Incorrect loop increments
5. Given arrival times [1000, 1010, 1025, 1030, 1040] and departure times [1020, 1035, 1045, 1050, 1100], what is the minimum number of platforms needed if trains can arrive and depart at the same time without conflict?