Bird
Raised Fist0
DSA C#programming~20 mins

Minimum Number of Platforms in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Platform Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the minimum number of platforms needed?
Given arrival and departure times of trains, what is the minimum number of platforms required so that no train waits?
DSA C#
int[] arrival = {900, 940, 950, 1100, 1500, 1800};
int[] departure = {910, 1200, 1120, 1130, 1900, 2000};

int FindPlatforms(int[] arr, int[] dep) {
    Array.Sort(arr);
    Array.Sort(dep);
    int platform_needed = 1, result = 1;
    int i = 1, j = 0;
    while (i < arr.Length && j < dep.Length) {
        if (arr[i] <= dep[j]) {
            platform_needed++;
            i++;
            if (platform_needed > result) result = platform_needed;
        } else {
            platform_needed--;
            j++;
        }
    }
    return result;
}

int answer = FindPlatforms(arrival, departure);
Console.WriteLine(answer);
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Sort arrival and departure times separately and compare them to find overlaps.
🧠 Conceptual
intermediate
1:30remaining
What does the variable 'platform_needed' represent in the algorithm?
In the minimum number of platforms algorithm, what does the variable 'platform_needed' track during execution?
AThe number of trains that have departed
BThe total number of trains processed so far
CThe maximum number of platforms used so far
DThe current number of trains at the station at the same time
Attempts:
2 left
💡 Hint
Think about what changes when a train arrives or departs.
🔧 Debug
advanced
2:00remaining
Identify the error in this platform calculation code snippet
What error will this code produce when run, and why?
DSA C#
int[] arrival = {900, 940, 950};
int[] departure = {910, 920};

int FindPlatforms(int[] arr, int[] dep) {
    Array.Sort(arr);
    Array.Sort(dep);
    int platform_needed = 1, result = 1;
    int i = 1, j = 0;
    while (i < arr.Length) {
        if (arr[i] <= dep[j]) {
            platform_needed++;
            i++;
            if (platform_needed > result) result = platform_needed;
        } else {
            platform_needed--;
            j++;
        }
    }
    return result;
}

int answer = FindPlatforms(arrival, departure);
Console.WriteLine(answer);
AOutput is 1
BIndexOutOfRangeException because departure array is shorter than arrival array
COutput is 2
DNo error, output is 3
Attempts:
2 left
💡 Hint
Check if arrays have the same length before processing.
❓ Predict Output
advanced
2:00remaining
Output of platform calculation with overlapping trains
What is the output of the following code?
DSA C#
int[] arrival = {1000, 1010, 1025, 1030, 1040};
int[] departure = {1020, 1035, 1045, 1050, 1100};

int FindPlatforms(int[] arr, int[] dep) {
    Array.Sort(arr);
    Array.Sort(dep);
    int platform_needed = 1, result = 1;
    int i = 1, j = 0;
    while (i < arr.Length && j < dep.Length) {
        if (arr[i] <= dep[j]) {
            platform_needed++;
            i++;
            if (platform_needed > result) result = platform_needed;
        } else {
            platform_needed--;
            j++;
        }
    }
    return result;
}

int answer = FindPlatforms(arrival, departure);
Console.WriteLine(answer);
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Count how many trains overlap at the busiest time.
🧠 Conceptual
expert
1:30remaining
Why sorting arrival and departure times separately is crucial in this algorithm?
Why do we sort arrival and departure arrays separately before calculating the minimum number of platforms?
ATo remove duplicate times and simplify the calculation
BTo ensure the arrays have the same length for iteration
CTo process events in chronological order and correctly count overlapping trains
DTo group all arrivals before departures for easier counting
Attempts:
2 left
💡 Hint
Think about how the algorithm compares arrival and departure times step by step.

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

  1. 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.
  2. Step 2: Identify the key output

    The output is the minimum number of platforms required to handle all trains without delay.
  3. Final Answer:

    The least number of platforms needed so no train waits -> Option B
  4. 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);
C. int[] arrival = {900, 940, 950}; int[] departure = {910, 1200, 1120};
D. int[] arrival = (900, 940, 950); int[] departure = (910, 1200, 1120);

Solution

  1. Step 1: Recall C# array declaration syntax

    In C#, arrays are declared with type[], and values inside curly braces {}.
  2. Step 2: Check each option for correct syntax

    int[] arrival = {900, 940, 950}; int[] departure = {910, 1200, 1120}; uses int[] and curly braces {}, which is correct. Others use parentheses or wrong syntax.
  3. Final Answer:

    int[] arrival = {900, 940, 950}; int[] departure = {910, 1200, 1120}; -> Option C
  4. Quick Check:

    C# arrays use type[] and {} [OK]
Hint: Use int[] and curly braces {} for arrays in C# [OK]
Common Mistakes:
  • Using parentheses () instead of curly braces {}
  • Declaring arrays without [] after type
  • Using square brackets [] for initialization
3. What is the output of the following C# code snippet that calculates minimum platforms?
int[] arrival = {900, 940, 950, 1100, 1500, 1800};
int[] departure = {910, 1200, 1120, 1130, 1900, 2000};

int platforms = CalculateMinPlatforms(arrival, departure);
Console.WriteLine(platforms);

// Assume CalculateMinPlatforms correctly implements the algorithm.
medium
A. 5
B. 4
C. 2
D. 3

Solution

  1. Step 1: Sort arrival and departure times

    Arrival: 900, 940, 950, 1100, 1500, 1800 Departure: 910, 1120, 1130, 1200, 1900, 2000
  2. Step 2: Count overlapping trains to find max platforms

    At 950, trains at 900, 940, 950 overlap (3 trains). Later overlaps are fewer.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Max overlap = 3 platforms [OK]
Hint: Find max overlapping intervals for minimum platforms [OK]
Common Mistakes:
  • Not sorting times before counting overlaps
  • Counting total trains instead of overlaps
  • Mixing arrival and departure arrays
4. Identify the error in this C# code snippet for calculating minimum platforms:
int[] arrival = {900, 940, 950};
int[] departure = {910, 1200, 1120};

Array.Sort(arrival);
Array.Sort(departure);

int i = 0, j = 0, platforms = 0, maxPlatforms = 0;
while (i < arrival.Length && j < departure.Length) {
    if (arrival[i] <= departure[j]) {
        platforms++;
        i++;
    } else {
        platforms--;
        j++;
    }
    maxPlatforms = Math.Max(maxPlatforms, platforms);
}
Console.WriteLine(maxPlatforms);
medium
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

  1. Step 1: Analyze the comparison operator in the loop

    Using '<=' causes counting a train as arriving exactly when another departs, increasing platforms unnecessarily.
  2. 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.
  3. Final Answer:

    The comparison should be 'arrival[i] < departure[j]' instead of '<=' -> Option A
  4. 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

  1. Step 1: Modify algorithm to track platform count changes

    While iterating, record start time when platforms reach max and end time when count drops.
  2. Step 2: Store intervals where platform count equals maximum

    Keep a list of intervals with max platforms to return after processing all times.
  3. Final Answer:

    Track intervals when platform count equals max during iteration and store them -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard