📖 Scenario: You are managing a small railway station. You want to find out the minimum number of platforms needed so that no train has to wait.
🎯 Goal: Build a Go program that calculates the minimum number of platforms required given arrival and departure times of trains.
📋 What You'll Learn
Create two slices called arrivals and departures with exact train times
Create an integer variable called platforms_needed to track platforms required
Write a loop to find the minimum number of platforms needed
Print the final platforms_needed value
💡 Why This Matters
🌍 Real World
Railway stations and airports use this logic to manage platforms and gates efficiently to avoid delays.
💼 Career
Understanding scheduling and resource allocation algorithms is important for software engineers working on transportation, logistics, and event management systems.
Progress0 / 4 steps
1
Create arrival and departure time slices
Create two slices called arrivals and departures with these exact values: arrivals = []int{900, 940, 950, 1100, 1500, 1800} and departures = []int{910, 1200, 1120, 1130, 1900, 2000}
DSA Go
Hint
Use Go slice syntax to create arrivals and departures with the exact times.
2
Create a variable to track platforms needed
Create an integer variable called platforms_needed and set it to 1 to start counting platforms
DSA Go
Hint
Initialize platforms_needed to 1 because at least one platform is needed initially.
3
Calculate minimum platforms needed
Write a loop using variables i and j to iterate over arrivals and departures. Use two counters i and j starting at 1 and 0 respectively. Use a variable count initialized to 1. Inside the loop, if arrivals[i] is less than or equal to departures[j], increment count and i. Else decrement count and increment j. Update platforms_needed to the maximum of itself and count. Stop when i reaches the length of arrivals.
DSA Go
Hint
Use two pointers i and j to compare arrivals and departures. Increase count when a train arrives before the previous departs, else decrease it.
4
Print the minimum number of platforms needed
Write fmt.Println(platforms_needed) to display the minimum number of platforms needed
DSA Go
Hint
Use fmt.Println(platforms_needed) to show the final answer.
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 earliest train arrival time
B. The total number of trains arriving in a day
C. The longest time a train stays on a platform
D. The least number of platforms needed so no train waits
Solution
Step 1: Understand the problem context
The problem wants to find how many platforms are needed so that no train has to wait for another to leave.
Step 2: Identify the key requirement
We must find the minimum number of platforms so that all trains can be accommodated without delay.
Final Answer:
The least number of platforms needed so no train waits -> Option D
Quick Check:
Minimum platforms = no waiting trains [OK]
Hint: Count overlapping trains to find minimum platforms needed [OK]
Common Mistakes:
Confusing total trains with platforms needed
Ignoring overlapping arrival and departure times
Thinking about train durations instead of platform count
2. Which Go code snippet correctly declares slices for arrival and departure times for the Minimum Number of Platforms problem?
3. What is the minimum number of platforms required for trains with arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]?
A. platforms should not be decremented when a train departs
B. The loop should run until i < len(arrivals) only
C. The comparison should be arrivals[i] < departures[j], not <=
D. platforms variable should be initialized to 1
Solution
Step 1: Understand platform counting logic
When arrival time equals departure time, we should consider the train has left before the next arrives to avoid extra platform count.
Step 2: Fix comparison operator
Using <= causes increment even when arrival equals departure, inflating platform count. Changing to < fixes this.
Final Answer:
The comparison should be arrivals[i] < departures[j], not <= -> Option C
Quick Check:
Use < to avoid counting simultaneous arrival/departure as overlap [OK]
Hint: Use strict less than (<) to avoid counting equal times as overlap [OK]
Common Mistakes:
Using <= instead of < in time comparison
Incorrect loop termination conditions
Wrong initialization of platform count
5. You have arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]. How would you modify the algorithm to also return the time intervals when the maximum number of platforms are needed?
hard
A. Track intervals when platform count reaches max during iteration
B. Sort only arrival times and print max arrival time intervals
C. Count total trains and divide by 2 to get intervals
D. Use a queue to store trains and print all arrival times
Solution
Step 1: Track platform count and max count during iteration
While iterating sorted arrivals and departures, keep updating current platform count and record when it reaches a new maximum.
Step 2: Record time intervals for max platforms
Store the start time when max platforms count is reached and end time when it decreases, to get intervals.
Final Answer:
Track intervals when platform count reaches max during iteration -> Option A
Quick Check:
Record max platform count intervals while counting platforms [OK]
Hint: Save start/end times when platform count hits max [OK]
Common Mistakes:
Ignoring departure times when tracking intervals
Only sorting arrivals without departures
Using total trains count instead of overlaps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.