Given arrays of arrival and departure times for trains at a station, find the minimum number of platforms required so that no train has to wait.
For example, given arrivals = [900, 940, 950, 1100, 1500, 1800] and departures = [910, 1200, 1120, 1130, 1900, 2000], the minimum platforms needed is 3.
Explain your approach and state the time and space complexity.
How to approach it
- Hint 1
Think about what happens at each point in time: a train arriving needs a platform, a train departing frees one.
- Hint 2
If you sort arrivals and departures independently, you can use two pointers to simulate the timeline.
- Hint 3
Advance the pointer with the smaller time. If it is an arrival, increment the platform count. If it is a departure, decrement. Track the maximum.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.