Bird
Raised Fist0
Interview PrepintervalsmediumAmazonGoogleMicrosoft

Minimum Number of Platforms Required

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Create arrival events

We start by creating events for each arrival time, marking them with +1 to indicate a train arriving.

💡 Separating arrivals as +1 events helps us later count how many trains are currently at the station.
Line:for time in arrivals: events.append((time, 1)) # arrival
💡 Each arrival increases the platform requirement by one when processed.
📊
Minimum Number of Platforms Required - Watch the Algorithm Execute, Step by Step
Watching the algorithm process each event one by one reveals how overlapping intervals cause platform requirements to increase and decrease, making the concept of sweep line intuitive.
Step 1/17
·Active fillAnswer cell
insert
i
(900,1)
0
(940,1)
1
(950,1)
2
(1100,1)
3
(1500,1)
4
(1800,1)
5
Result: "events = [(900,1), (940,1), (950,1), (1100,1), (1500,1), (1800,1)]"
insert
(900,1)
0
(940,1)
1
(950,1)
2
(1100,1)
3
(1500,1)
4
(1800,1)
5
i
(910,-1)
6
(1200,-1)
7
(1120,-1)
8
(1130,-1)
9
(1900,-1)
10
(2000,-1)
11
Result: "events = arrivals + departures (unsorted)"
sort
i
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "events sorted by time and type"
initialize
i
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=0, max_platforms=0"
compare
i
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=1, max_platforms=1"
compare
(900,1)
0
i
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=0, max_platforms=1"
compare
(900,1)
0
(910,-1)
1
i
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=1, max_platforms=1"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
i
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=2, max_platforms=2"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
i
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=3, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
i
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=2, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
i
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=1, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
i
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=0, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
i
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=1, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
i
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=2, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
i
(1900,-1)
10
(2000,-1)
11
Result: "platforms_needed=1, max_platforms=3"
compare
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
i
(2000,-1)
11
Result: "platforms_needed=0, max_platforms=3"
record
(900,1)
0
(910,-1)
1
(940,1)
2
(950,1)
3
(1100,1)
4
(1120,-1)
5
(1130,-1)
6
(1200,-1)
7
(1500,1)
8
(1800,1)
9
(1900,-1)
10
(2000,-1)
11
Result: 3

Key Takeaways

Sorting events by time and prioritizing departures before arrivals at the same time is crucial.

This prevents overcounting platforms when trains arrive and depart simultaneously, which is not obvious from code alone.

The sweep line approach converts interval overlap into a simple running count problem.

Visualizing events as points on a timeline helps understand how overlaps translate to platform needs.

Tracking both current platforms needed and maximum platforms needed separately is essential.

The maximum platforms needed is the final answer, but the current count fluctuates as events are processed.

Practice

(1/5)
1. Consider the following Python function for interval intersections:
from typing import List

def intervalIntersection(A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
    i, j = 0, 0
    result = []
    while i < len(A) and j < len(B):
        if A[i][1] < B[j][0]:
            i += 1
        elif B[j][1] < A[i][0]:
            j += 1
        else:
            start = max(A[i][0], B[j][0])
            end = min(A[i][1], B[j][1])
            result.append([start, end])
            if A[i][1] < B[j][1]:
                i += 1
            else:
                j += 1
    return result

A = [[0,2],[5,10],[13,23],[24,25]]
B = [[1,5],[8,12],[15,24],[25,26]]
print(intervalIntersection(A, B))
What is the final output printed by this code?
easy
A. [[1,2],[5,5],[8,10],[15,23],[24,25],[25,26]]
B. [[1,2],[5,5],[8,10],[15,23],[24,25],[25,25]]
C. [[1,2],[5,5],[8,10],[15,23],[24,24],[25,26]]
D. [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

Solution

  1. Step 1: Trace first intersection

    Compare [0,2] and [1,5]: overlap is [1,2], add to result.
  2. Step 2: Advance pointers and find all intersections

    Following pointer moves and overlaps yield [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Matches manual step-by-step intersection results [OK]
Hint: Trace pointer increments carefully to avoid off-by-one errors [OK]
Common Mistakes:
  • Including intervals beyond intersection
  • Merging adjacent but non-overlapping intervals
  • Off-by-one in pointer increments
2. What is the time complexity of the optimal car pooling solution using the difference array and prefix sum approach, given n trips and maximum location L?
medium
A. O(n * L) because we must simulate passenger count at every location for each trip
B. O(n + L) because we process each trip once and then sweep through the difference array once
C. O(n log n) due to sorting trips by start location before processing
D. O(L) because the difference array size dominates and trips are processed in constant time

Solution

  1. Step 1: Analyze trip processing

    Each of the n trips updates two positions in the difference array -> O(n)
  2. Step 2: Analyze prefix sum sweep

    We iterate over the difference array of size L once -> O(L)
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sum of O(n) + O(L) operations [OK]
Hint: Difference array updates O(n), prefix sum O(L) [OK]
Common Mistakes:
  • Confusing brute force O(n*L) with optimal
  • Assuming sorting is required
  • Ignoring prefix sum sweep cost
3. What is the time complexity of the optimal sweep line algorithm for Employee Free Time, given N total intervals across all employees?
medium
A. O(N log N) due to sorting all interval endpoints
B. O(N log M) where M is number of employees
C. O(N) because each interval is processed once
D. O(N²) due to nested iteration over intervals

Solution

  1. Step 1: Identify main operations

    We create 2N events (start and end per interval) and sort them.
  2. Step 2: Sorting events dominates complexity

    Sorting 2N events takes O(N log N). The sweep through events is O(N).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting dominates, so O(N log N) [OK]
Hint: Sorting interval endpoints dominates time complexity [OK]
Common Mistakes:
  • Assuming linear time because each interval processed once
4. The following code attempts to determine if a person can attend all meetings. Identify the line containing the subtle bug that causes incorrect results for intervals that just touch (e.g., [1,5] and [5,10]).
from typing import List

def can_attend_meetings(intervals: List[List[int]]) -> bool:
    intervals.sort(key=lambda x: x[0])
    end = float('-inf')
    for interval in intervals:
        if interval[0] <= end:
            return False
        end = interval[1]
    return True
medium
A. Line 6: if interval[0] <= end:
B. Line 3: intervals.sort(key=lambda x: x[0])
C. Line 5: for interval in intervals:
D. Line 7: end = interval[1]

Solution

  1. Step 1: Understand the overlap condition

    Meetings that just touch (e.g., end=5 and start=5) should not be considered overlapping.
  2. Step 2: Identify the bug

    The condition uses <= which incorrectly treats touching intervals as overlapping. It should use < instead.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Changing <= to < fixes false positives on touching intervals [OK]
Hint: Overlap check must use <, not <= [OK]
Common Mistakes:
  • Using <= causes false overlap detection on touching intervals
5. Examine the following buggy code for the Meeting Rooms II problem. Which line contains the subtle bug that can cause incorrect room count?
medium
A. Line 3: Missing check for empty intervals list.
B. Line 9: Incorrect condition using >= instead of > for overlap.
C. Line 11: Using heappush without popping first.
D. Line 6: Intervals are not sorted before processing.

Solution

  1. Step 1: Identify missing sorting.

    Intervals must be sorted by start time before heap processing; missing this breaks heap logic.
  2. Step 2: Confirm other lines.

    Empty check is present; condition >= is correct for allowing room reuse; pushing after popping is correct.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Without sorting, heap logic fails to track earliest ending meeting [OK]
Hint: Heap logic requires sorted intervals by start time [OK]
Common Mistakes:
  • Confusing overlap condition with <=
  • Forgetting to sort intervals
  • Misordering heap push/pop