Minimum Platforms
Problem Statement
Train i arrives at a station at time arr[i] and leaves at time dep[i]. Times are written as 24-hour HHMM numbers, e.g. 950 is 9:50 and 1120 is 11:20. All trains arrive and leave on the same day.
A train occupies a platform from its arrival minute to its departure minute, both included. So if one train leaves at 1120 and another arrives at 1120, both are at the station at 1120 and they need different platforms.
Implement int findPlatform(vector<int>& arr, vector<int>& dep) that returns the smallest number of platforms so that no train ever has to wait.
Two arrays of the same length: arr (arrival times) and dep (departure times). arr[i] ≤ dep[i].
One integer: the minimum number of platforms.
Around 11:00, the trains of 940–1200, 950–1120 and 1100–1130 are all at the station at once.
No two trains are at the station at the same time, so one platform is enough.
At 1000 the first train is still at its platform (it leaves at 1000) while the second one arrives. Times are inclusive, so they need two platforms.
- • 1 ≤ n ≤ 5 · 10^4
- • 0000 ≤ arr[i] ≤ dep[i] ≤ 2359
- • arr and dep are not necessarily sorted
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each arrival time, count the trains at the station at that minute
Two sorted lists; at station = arrived − left, with left counting only dep < arr
The answer is the most trains at the station at once, and that count only rises at an arrival. So for each train i, count trains j with arr[j] ≤ arr[i] ≤ dep[j] (arrived, and not yet left, inclusive). Return the biggest count.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.