Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Minimum Number of Platforms in DSA Python - 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
Minimum Platforms Needed for Given Train Schedule
What is the output of the following code that calculates the minimum number of platforms needed for trains arriving and departing at given times?
DSA Python
def find_min_platforms(arrivals, departures):
    arrivals.sort()
    departures.sort()
    platform_needed = 0
    max_platforms = 0
    i = 0
    j = 0
    n = len(arrivals)
    while i < n and j < n:
        if arrivals[i] < departures[j]:
            platform_needed += 1
            max_platforms = max(max_platforms, platform_needed)
            i += 1
        else:
            platform_needed -= 1
            j += 1
    return max_platforms

arrivals = [900, 940, 950, 1100, 1500, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000]
print(find_min_platforms(arrivals, departures))
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Sort arrival and departure times, then count platforms needed by comparing arrivals and departures.
🧠 Conceptual
intermediate
1:30remaining
Understanding Platform Calculation Logic
In the minimum number of platforms problem, why do we sort both arrival and departure times before processing?
ATo group all arrivals before departures for easier counting
BTo process events in chronological order and correctly count overlapping trains
CTo match each arrival with its exact departure time
DTo ensure departures are always processed before arrivals
Attempts:
2 left
💡 Hint
Think about how sorting helps in comparing times step-by-step.
❓ Predict Output
advanced
2:00remaining
Output of Platform Calculation with Overlapping Times
What is the output of this code snippet calculating minimum platforms for overlapping train times?
DSA Python
arrivals = [1000, 1010, 1025, 1030]
departures = [1020, 1035, 1040, 1050]

def min_platforms(arr, dep):
    arr.sort()
    dep.sort()
    i = j = 0
    count = max_count = 0
    n = len(arr)
    while i < n and j < n:
        if arr[i] < dep[j]:
            count += 1
            max_count = max(max_count, count)
            i += 1
        else:
            count -= 1
            j += 1
    return max_count

print(min_platforms(arrivals, departures))
A1
B4
C2
D3
Attempts:
2 left
💡 Hint
Check how many trains overlap at the same time.
🔧 Debug
advanced
2:00remaining
Identify the Error in Platform Calculation Code
What error does this code produce when calculating minimum platforms?
DSA Python
def min_platforms(arrivals, departures):
    arrivals.sort()
    departures.sort()
    i = j = 0
    count = max_count = 0
    n = len(arrivals)
    while i < n and j < n:
        if arrivals[i] < departures[j]:
            count += 1
            max_count = max(max_count, count)
            i += 1
        else:
            count -= 1
            j += 1
    return max_count

arrivals = [900, 940, 950]
departures = [910, 1200, 1120]
print(min_platforms(arrivals, departures))
AIndexError: list index out of range
BTypeError: unsupported operand type(s)
C0
D3
Attempts:
2 left
💡 Hint
Check loop conditions and index usage carefully.
🚀 Application
expert
3:00remaining
Calculate Minimum Platforms for Large Schedule
Given the following arrival and departure times, what is the minimum number of platforms needed?
DSA Python
arrivals = [900, 940, 950, 1100, 1500, 1800, 1830, 1845, 1900, 1930]
departures = [910, 1200, 1120, 1130, 1900, 2000, 1850, 1905, 1950, 2000]

def min_platforms(arr, dep):
    arr.sort()
    dep.sort()
    i = j = 0
    count = max_count = 0
    n = len(arr)
    while i < n and j < n:
        if arr[i] < dep[j]:
            count += 1
            max_count = max(max_count, count)
            i += 1
        else:
            count -= 1
            j += 1
    return max_count

print(min_platforms(arrivals, departures))
A6
B5
C4
D3
Attempts:
2 left
💡 Hint
Sort and count overlapping trains carefully.

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

  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 requirement

    We must find the minimum number of platforms so that all trains can be accommodated without delay.
  3. Final Answer:

    The least number of platforms needed so no train waits -> Option A
  4. Quick Check:

    Minimum platforms = no waiting trains [OK]
Hint: Focus on overlapping train times to find platform count [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 of the following Python code snippets correctly sorts the arrival times list named arrivals for the Minimum Number of Platforms problem?
easy
A. arrivals.sort(arrivals)
B. sort(arrivals)
C. arrivals.sort()
D. arrivals.sorted()

Solution

  1. Step 1: Recall Python list sorting method

    Python lists have a method called sort() that sorts the list in place.
  2. Step 2: Check each option

    arrivals.sort() uses arrivals.sort() which is correct. sort(arrivals) is invalid syntax. arrivals.sorted() uses a non-existent method. arrivals.sort(arrivals) incorrectly passes the list as argument.
  3. Final Answer:

    arrivals.sort() -> Option C
  4. Quick Check:

    List.sort() is correct method [OK]
Hint: Use list.sort() to sort lists in place [OK]
Common Mistakes:
  • Using sort() as a standalone function
  • Trying to call sorted() as a method
  • Passing the list as argument to sort()
3. What is the minimum number of platforms needed for the following train schedule?
arrivals = [900, 940, 950, 1100, 1500, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000]
medium
A. 4
B. 2
C. 3
D. 1

Solution

  1. Step 1: Sort arrival and departure times

    Arrivals sorted: [900, 940, 950, 1100, 1500, 1800]
    Departures sorted: [910, 1120, 1130, 1200, 1900, 2000]
  2. Step 2: Use two pointers to count platforms

    Start with i=0 (arrivals), j=0 (departures), platforms=0, max_platforms=0.
    At 900 (arrival), platforms=1.
    At 910 (departure), platforms=0.
    At 940 (arrival), platforms=1.
    At 950 (arrival), platforms=2.
    At 1100 (arrival), platforms=2 (since departure at 1120 is after 1100 but 1130 and 1200 are later).
    Max platforms needed = 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Max overlapping trains = 2 [OK]
Hint: Count overlapping trains using sorted arrivals and departures [OK]
Common Mistakes:
  • Not sorting times before counting
  • Mixing arrival and departure pointers
  • Counting platforms incorrectly at overlaps
4. Identify the error in this Python code snippet for finding minimum platforms:
arrivals = [900, 940, 950]
departures = [910, 1200, 1120]
arrivals.sort()
departures.sort()
i = j = 0
platforms = max_platforms = 0
while i < len(arrivals) and j < len(departures):
if arrivals[i] <= departures[j]:
platforms += 1
i += 1
else:
platforms -= 1
j += 1
max_platforms = max(max_platforms, platforms)
print(max_platforms)
medium
A. The comparison should be arrivals[i] < departures[j], not <=.
B. The loop condition should use 'or' instead of 'and'.
C. The platforms variable should not be incremented.
D. The departures list should not be sorted.

Solution

  1. Step 1: Understand the comparison logic

    Using <= means if arrival time equals departure time, it counts as needing a new platform, but usually a train leaving at time t frees the platform for a train arriving at time t.
  2. Step 2: Correct the comparison

    Changing <= to < ensures that if arrival equals departure, platform count decreases first, avoiding unnecessary extra platform.
  3. Final Answer:

    The comparison should be arrivals[i] < departures[j], not <=. -> Option A
  4. Quick Check:

    Use < to avoid counting simultaneous arrival and departure as overlap [OK]
Hint: Use strict less than for arrival vs departure comparison [OK]
Common Mistakes:
  • Using <= causes overcounting platforms
  • Changing loop condition wrongly to 'or'
  • Not sorting departure times
5. Given multiple trains with arrival and departure times, how would you efficiently find the minimum number of platforms needed if the times are in 24-hour format and some trains arrive and depart at the same time? For example:
arrivals = [900, 940, 950, 1100, 1500, 1800, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000, 1800]

Choose the best approach.
hard
A. Use a dictionary to map arrival times to departure times and count keys.
B. Count trains arriving at each time and sum them directly without sorting.
C. Sort arrivals only and count unique times.
D. Sort arrivals and departures, then use two pointers with strict less than comparison for arrival vs departure.

Solution

  1. Step 1: Sort both arrival and departure lists

    Sorting helps to process events in chronological order, which is essential for counting overlapping trains.
  2. Step 2: Use two pointers with strict comparison

    Use two pointers to traverse arrivals and departures. Use arrivals[i] < departures[j] to decide when to increase or decrease platform count, handling same-time arrivals and departures correctly.
  3. Final Answer:

    Sort arrivals and departures, then use two pointers with strict less than comparison for arrival vs departure. -> Option D
  4. Quick Check:

    Sorting + two pointers + < comparison = correct approach [OK]
Hint: Sort both lists and use two pointers with strict comparison [OK]
Common Mistakes:
  • Ignoring sorting of departures
  • Using <= causing overcount
  • Trying to count without sorting or pointers
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