📖 Scenario: You are managing a small train station. You want to find out the minimum number of platforms needed so that no train has to wait.Each train has an arrival time and a departure time.
🎯 Goal: Build a program that calculates the minimum number of platforms required for the trains given their arrival and departure times.
📋 What You'll Learn
Create two arrays 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 using the arrival and departure arrays.
Print the final number of platforms needed.
💡 Why This Matters
🌍 Real World
Train stations and airports use this logic to manage platforms or gates efficiently to avoid delays.
💼 Career
This problem teaches scheduling and resource allocation skills useful in software engineering and operations roles.
Progress0 / 4 steps
1
Create train arrival and departure arrays
Create two integer arrays called arrivals and departures with these exact values: arrivals = new int[] {900, 940, 950, 1100, 1500, 1800}; departures = new int[] {910, 1200, 1120, 1130, 1900, 2000};
DSA C#
Hint
Use int[] to create arrays and assign the exact times given.
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 C#
Hint
Start with 1 platform since at least one train will arrive.
3
Calculate minimum platforms needed
Write code to calculate the minimum number of platforms needed. First, sort the arrivals and departures arrays. Then use two integer variables i and j starting at 1 and 0 respectively. Use a while loop that runs while i < arrivals.Length and j < departures.Length. Inside the loop, if arrivals[i] <= departures[j], increment platforms_needed and i. Else, decrement platforms_needed and increment j. Keep track of the maximum platforms needed in an integer variable called max_platforms.
DSA C#
Hint
Sort the arrays first. Use two pointers to compare arrival and departure times. Increase or decrease platforms needed accordingly. Track the maximum platforms needed.
4
Print the minimum number of platforms needed
Print the value of max_platforms to show the minimum number of platforms required.
DSA C#
Hint
Use Console.WriteLine(max_platforms); to print 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 total time trains spend at the station
B. The least number of platforms needed so no train waits
C. The maximum speed of any train
D. The total number of trains arriving in a day
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 output
The output is the minimum number of platforms required to handle all trains without delay.
Final Answer:
The least number of platforms needed so no train waits -> Option B
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 speed or duration instead
2. Which of the following C# code snippets correctly declares arrays for arrival and departure times for the Minimum Number of Platforms problem?
easy
A. int arrival = [900, 940, 950]; int departure = [910, 1200, 1120];
B. int arrival[] = (900, 940, 950); int departure[] = (910, 1200, 1120);
A. The comparison should be 'arrival[i] < departure[j]' instead of '<='
B. The loop condition should be 'i < arrival.Length || j < departure.Length'
C. The arrays should not be sorted before processing
D. The platforms variable should not be incremented
Solution
Step 1: Analyze the comparison operator in the loop
Using '<=' causes counting a train as arriving exactly when another departs, increasing platforms unnecessarily.
Step 2: Correct comparison to '<' to avoid overlap at exact departure time
Changing to '<' means a train arriving at the same time another departs does not increase platform count.
Final Answer:
The comparison should be 'arrival[i] < departure[j]' instead of '<=' -> Option A
Quick Check:
Use '<' to avoid counting simultaneous arrival and departure as overlap [OK]
Hint: Use '<' not '<=' when comparing arrival and departure [OK]
Common Mistakes:
Using '<=' causes overcounting platforms
Not sorting arrays before processing
Incorrect loop condition causing missed trains
5. Given 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 equals max during iteration and store them
B. Return only the maximum platform count without intervals
C. Sort only arrival times and ignore departure times
D. Count total trains without checking overlaps
Solution
Step 1: Modify algorithm to track platform count changes
While iterating, record start time when platforms reach max and end time when count drops.
Step 2: Store intervals where platform count equals maximum
Keep a list of intervals with max platforms to return after processing all times.
Final Answer:
Track intervals when platform count equals max during iteration and store them -> Option A
Quick Check:
Record max platform intervals during count updates [OK]
Hint: Save start/end times when platform count hits max [OK]
Common Mistakes:
Ignoring time intervals, returning only counts
Not updating intervals when platform count changes
Sorting only one array causing wrong intervals
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.