Bird
Raised Fist0
DSA Cprogramming~10 mins

Minimum Number of Platforms in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Minimum Number of Platforms
Sort arrival times
↓
Sort departure times
↓
Initialize pointers i=0, j=0
↓
Compare arrival[i
↓
Increment platforms
↓
Track max platforms needed
↓
Repeat until all trains processed
Sort arrival and departure times, then use two pointers to count platforms needed by comparing arrivals and departures.
Execution Sample
DSA C
int minPlatforms(int arr[], int dep[], int n) {
  sort(arr, arr+n);
  sort(dep, dep+n);
  int i = 0, j = 0, platforms = 0, maxPlatforms = 0;
  while (i < n && j < n) {
    if (arr[i] <= dep[j]) platforms++, i++;
    else platforms--, j++;
    maxPlatforms = max(maxPlatforms, platforms);
  }
  return maxPlatforms;
}
Calculates minimum platforms needed by sorting times and counting overlaps.
Execution Table
StepOperationi (arrival index)j (departure index)Platforms CountMax PlatformsVisual State (Arrivals vs Departures)
1Sort arrival and departure arrays0000Arrivals: [900, 940, 950, 1100, 1500, 1800] Departures: [910, 1120, 1130, 1200, 1900, 2000]
2Compare arr[0]=900 <= dep[0]=9100011Platform needed for train arriving at 900
3Increment i to 11011Platforms still 1
4Compare arr[1]=940 <= dep[0]=9101001Train at 940 arrives after 910 departure, platform freed
5Increment j to 11101Platforms now 0
6Compare arr[1]=940 <= dep[1]=11201111New train arrives before next departure, platform needed
7Increment i to 22111Platforms 1
8Compare arr[2]=950 <= dep[1]=11202122Another train arrives, platform count increases
9Increment i to 33122Platforms 2
10Compare arr[3]=1100 <= dep[1]=11203133Third train overlaps, platform count 3
11Increment i to 44133Platforms 3
12Compare arr[4]=1500 <= dep[1]=11204123Train arrives after departure, platform freed
13Increment j to 24223Platforms 2
14Compare arr[4]=1500 <= dep[2]=11304213Another departure frees platform
15Increment j to 34313Platforms 1
16Compare arr[4]=1500 <= dep[3]=12004303Departure frees platform
17Increment j to 44403Platforms 0
18Compare arr[4]=1500 <= dep[4]=19004413Train arrives before departure, platform needed
19Increment i to 55413Platforms 1
20Compare arr[5]=1800 <= dep[4]=19005423Another train arrives, platform count 2
21Increment i to 66423Platforms 2
22i reached n=6, loop ends6423All trains processed
23Return maxPlatforms---3Minimum platforms needed is 3
💡 All trains processed (i reached n=6), loop ends
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8After Step 10After Step 12After Step 18After Step 20Final
i0112344566
j0011112444
platforms0101232122
maxPlatforms0111233333
Key Moments - 3 Insights
Why do we increment 'j' (departure pointer) only when arrival[i] > departure[j]?
Because when a train arrives after another departs, a platform frees up, so we move the departure pointer to check next departure. This is shown in execution_table steps 4, 12, and 16 where platforms decrease and j increments.
Why do we track maxPlatforms separately from current platforms?
Current platforms count changes as trains arrive and depart, but maxPlatforms keeps the highest number seen so far. This ensures we know the maximum platforms needed at any time, as seen in steps 10 and 20.
What happens if arrival and departure times are equal?
If arrival[i] == departure[j], we treat it as arrival <= departure, so platforms increase. This ensures a platform is ready for the arriving train, as in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the platforms count at step 10?
A1
B2
C3
D0
💡 Hint
Check the 'Platforms Count' column at step 10 in the execution_table.
At which step does the departure pointer 'j' first increment?
AStep 5
BStep 3
CStep 7
DStep 9
💡 Hint
Look for the first step where 'j' changes from 0 to 1 in variable_tracker.
If the arrival time at index 1 was 920 instead of 940, how would platforms count change at step 4?
AIt would increase to 1
BIt would remain 0
CIt would decrease to -1
DIt would increase to 2
💡 Hint
Compare arrival[1] with departure[0] at step 4 in execution_table and consider the condition arr[i] <= dep[j].
Concept Snapshot
Minimum Number of Platforms:
- Sort arrival and departure times separately.
- Use two pointers i, j for arrivals and departures.
- If arrival[i] <= departure[j], increment platforms and i.
- Else decrement platforms and increment j.
- Track max platforms during iteration.
- Result is max platforms needed to avoid waiting.
Full Transcript
To find the minimum number of platforms needed for trains, we first sort the arrival and departure times. We use two pointers: one for arrivals and one for departures. We compare the current arrival time with the current departure time. If the arrival is earlier or equal, it means a train needs a platform, so we increase the platform count and move to the next arrival. If the arrival is later, it means a train has left, freeing a platform, so we decrease the platform count and move to the next departure. We keep track of the maximum number of platforms needed at any time. When all trains are processed, this maximum is the minimum number of platforms required to avoid waiting.

Practice

(1/5)
1. What does the problem "Minimum Number of Platforms" ask you to find given train arrival and departure times?
easy
A. The earliest train arrival time
B. The minimum number of platforms needed so no train waits
C. The total number of trains arriving in a day
D. The maximum number of trains arriving at the same time

Solution

  1. 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.
  2. Step 2: Clarify what is being counted

    We count how many trains overlap in time, which determines how many platforms are needed simultaneously.
  3. Final Answer:

    The minimum number of platforms needed so no train waits -> Option B
  4. 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 only arrivals or departures
2. Which of the following C code snippets correctly declares arrays for train arrival and departure times for 5 trains?
easy
A. int arrival[5] = {900, 940, 950, 1100, 1500}; int departure[5] = {910, 1200, 1120, 1130, 1900};
B. int arrival = {900, 940, 950, 1100, 1500}; int departure = {910, 1200, 1120, 1130, 1900};
C. int arrival[5] = (900, 940, 950, 1100, 1500); int departure[5] = (910, 1200, 1120, 1130, 1900);
D. int arrival[] = 900, 940, 950, 1100, 1500; int departure[] = 910, 1200, 1120, 1130, 1900;

Solution

  1. Step 1: Check array declaration syntax in C

    Arrays must be declared with size or empty brackets and initialized with curly braces {}.
  2. Step 2: Validate each option

    int arrival[5] = {900, 940, 950, 1100, 1500}; int departure[5] = {910, 1200, 1120, 1130, 1900}; uses correct syntax with size and braces. Others use incorrect syntax like parentheses or missing braces.
  3. Final Answer:

    int arrival[5] = {900, 940, 950, 1100, 1500}; int departure[5] = {910, 1200, 1120, 1130, 1900}; -> Option A
  4. Quick Check:

    Arrays use {} with size or empty brackets [OK]
Hint: Use curly braces {} to initialize arrays in C [OK]
Common Mistakes:
  • Using parentheses () instead of braces {}
  • Missing array size or empty brackets
  • Assigning values without braces
3. Given arrival times {900, 940, 950, 1100, 1500} and departure times {910, 1200, 1120, 1130, 1900}, what is the minimum number of platforms needed? Consider the following code snippet:
int arrival[] = {900, 940, 950, 1100, 1500};
int departure[] = {910, 1200, 1120, 1130, 1900};
int n = 5;

// Assume arrays are sorted
int i = 1, j = 0, platforms = 1, result = 1;
while (i < n && j < n) {
  if (arrival[i] <= departure[j]) {
    platforms++;
    i++;
    if (platforms > result) result = platforms;
  } else {
    platforms--;
    j++;
  }
}
printf("%d", result);
medium
A. 4
B. 2
C. 1
D. 3

Solution

  1. Step 1: Trace the platform count with given times

    Starts platforms=1 (900 arrival), i=1, j=0. 940 <= 910? No, so platforms--, j=1. Then 940 <= 1200: platforms++, i=2. 950 <= 1200: platforms++, i=3. 1100 <= 1200: platforms++, i=4 (max=3). Later departures reduce platforms.
  2. Step 2: Find max platforms during iteration

    Maximum platforms reached is 3 (trains 940-1200, 950-1120, 1100-1130 overlap).
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Max overlapping trains = 3 [OK]
Hint: Sort times and count overlaps to find max platforms [OK]
Common Mistakes:
  • Not sorting arrival and departure arrays
  • Incorrectly incrementing or decrementing platform count
  • Confusing <= with < in comparisons
4. Identify the error in the following code snippet for calculating minimum platforms:
int arrival[] = {900, 940, 950, 1100, 1500};
int departure[] = {910, 1200, 1120, 1130, 1900};
int n = 5;
int i = 0, j = 0, platforms = 0, result = 0;
while (i < n && j < n) {
  if (arrival[i] < departure[j]) {
    platforms++;
    i++;
    if (platforms > result) result = platforms;
  } else {
    platforms--;
    j++;
  }
}
printf("%d", result);
medium
A. No error, code is correct
B. Initialization of platforms should start at 1
C. Loop condition should be i <= n && j <= n
D. Comparison should be arrival[i] <= departure[j]

Solution

  1. Step 1: Verify initialization and first steps

    platforms=0, i=0, j=0 correct. 900 < 910: platforms=1, i=1, result=1.
  2. Step 2: Check logic and trace

    940 < 910? No: platforms=0, j=1. Then 940 < 1200: platforms=1, i=2; 950<1200:2, i=3; 1100<1200:3, i=4 (max=3). Continues correctly. < handles equal times optimally.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Code outputs 3, matching overlaps [OK]
Hint: plat=0, i=0,j=0, if arr[i] < dep[j] correctly minimizes platforms [OK]
Common Mistakes:
  • Using arrival[i] <= departure[j] (overcounts on equal times)
  • Initializing platforms=1 with i=0 (overcounts first arrival)
  • Loop i <= n && j <= n (off-by-one)
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 handle trains arriving and departing at the same time (e.g., arrival[i] == departure[j]) to minimize platforms needed?
hard
A. Ignore trains with equal arrival and departure times
B. Treat arrival[i] == departure[j] as arrival first to allocate platform
C. Treat arrival[i] == departure[j] as departure first to free platform
D. Increase platform count by 2 when times are equal

Solution

  1. Step 1: Understand impact of equal arrival and departure times

    If a train departs at the same time another arrives, the platform can be reused without adding a new one.
  2. Step 2: Adjust comparison logic

    When arrival[i] == departure[j], count departure first to free platform before arrival increments platform count.
  3. Final Answer:

    Treat arrival[i] == departure[j] as departure first to free platform -> Option C
  4. Quick Check:

    Departure before arrival frees platform [OK]
Hint: Count departures before arrivals when times are equal [OK]
Common Mistakes:
  • Counting arrival before departure causing extra platform
  • Ignoring equal times leading to wrong count
  • Adding platforms unnecessarily
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