Scrivio Interview library
Sorting and intervalsGreedy algorithmsReported 46 times · 14 min read · updated 15 November 2026

Given train arrival and departure times, find the minimum number of platforms a station needs so no train waits.

1What the station problem really asks

Imagine someone at the door of a hall with a clicker counter. Each time a guest walks in, they click up; each time a guest walks out, they click down. They do not need to know anyone's name. At the end of the evening, the highest number the clicker ever showed is the number of chairs the hall needed. Keep this clicker in mind, because it is exactly the tool we will build.

In the station problem we are given two arrays for one day: arrive[i] is when train i comes in and depart[i] is when it leaves. While a train is in the station it occupies one platform, and no train may wait for one. We must find the smallest number of platforms that makes this possible. That sounds like a puzzle about assigning platforms, but it is smaller than it looks. If at some moment k trains stand in the station together, we need at least k platforms. And if no moment ever has more than k, then k platforms are enough, because whenever a train arrives fewer than k are busy and a free one is waiting. So the answer is simply the largest number of trains present at the same moment.

Our example has six trains, with times written as hhmm, so 1025 means 10:25. They arrive at 1000, 1010, 1025, 1100, 1105 and 1300 and leave at 1030, 1020, 1110, 1115, 1200 and 1310, in the same order. Work it by hand. At 1010 the first two trains are in. At 1020 the second leaves. At 1025 a new one arrives, and at 1030 the first leaves. At 1100 another arrives, and at 1105 one more, while the trains from 1025 and 1100 are still there. That is three trains at once. After that they leave one by one before the last arrives at 1300, so the answer is 3.

PictureSix trains: arrival on top, departure below

Each column is one train, with its arrival time in the top row and its departure time in the bottom row, written as hhmm.

  1. 100010101025110011051300103010201110111512001310
    trains
    6
    in at 1105
    3 trains
    answer
    3 platforms

    1At 1105 the trains that arrived at 1025, 1100 and 1105 are all standing in the station, so at least three platforms are needed.

What to assume

If one train arrives at the same minute another departs, can they share a platform?
We assume not: both are in the station during that minute, so both need one.
Are all times on the same day, with no departure before its own arrival?
Yes.
Can the schedule be empty?
Yes, and then the answer is 0.

A careful candidate settles a few details first. If one train arrives at the same minute another departs, can they share a platform? We assume not: both are in the station during that minute, so both need one, and we will see that the other rule is a one character change. Are all times on the same day, with no departure before its own arrival? Yes. Can the schedule be empty? Yes, and then the answer is 0.

2How to recognise a sorting and intervals problem

A beginner reading this question for the first time often tries to simulate the station: put the first train on platform 1, then decide for the next train whether platform 1 is free, and so on. That can work, but it makes us track which train is where, and the bookkeeping grows quickly. Let us instead read the question for clues.

The first clue is that each train is described by a start time and an end time. A pair like that is called an interval, a stretch of time with a beginning and an end. Whenever a problem hands us a list of intervals, it is worth asking whether putting them in time order, that is sorting them, makes the question easy.

The second clue is the word minimum together with the idea of things happening at the same time. As we saw, the minimum number of platforms equals the maximum number of intervals that overlap at one moment. So the question is really how many intervals cover the busiest point in time. That is a question we can answer by walking through time from left to right and counting, the way the clicker does.

These clues point to the pattern called sorting and intervals. Its idea is to sort the starts and ends of the intervals by time and then sweep through them in order, like a line moving across a timetable, updating a running count at each start or end. A start or an end is called an event. The general rule to carry to new problems is this: when intervals compete for a shared resource and you need the most that are active at once, turn each interval into a start event and an end event, sort the events, and sweep with a running count.

Similar interval problems ask different questions and need different sorts. Merging overlapping intervals sorts by start and joins neighbours. Choosing the most meetings that do not overlap sorts by end time and picks greedily. Both sort intervals, but neither counts overlaps, which is what we need here.

3The honest first attempt, counting at every arrival

The first solution that works uses a simple fact. The number of trains in the station only goes up when a train arrives, so the busiest moment always happens at some arrival time. So we can stand at each arrival moment, count how many trains are in the station then, and keep the largest count. A train j is in the station at time t when it has arrived by then and not yet left, arrive[j] <= t <= depart[j]. This brute force is correct and easy to trust.

Java
static int minPlatformsBrute(int[] arrive, int[] depart) {
int most = 0;
for (int i = 0; i < arrive.length; i++) { // stand at each arrival moment
int present = 0;
for (int j = 0; j < arrive.length; j++) // and look at every train again
if (arrive[j] <= arrive[i] && arrive[i] <= depart[j]) present++; // j is in the station
most = Math.max(most, present);
}
return most;
}

Let us run it by hand. At 1000 only the first train is in, a count of 1. At 1010 two trains, at 1025 two, at 1100 two, at 1105 three, and at 1300 one. The largest is 3, the right answer. To get those six numbers, the program checked every train at every arrival, 6 times 6, which is 36 checks.

In general, n arrivals times n trains is n squared checks, so the time is O(n^2) and the extra space is O(1). For a busy rail network with 30,000 trains a day, that is 900 million checks.

Now let us find the waste. Look at the counts in order: 1, 2, 2, 2, 3, 1. From one arrival to the next, the count changes only because of the few trains that arrived or left in between, yet each count starts again from zero and looks at all n trains. The clicker at the door never does that. It keeps one running number and adjusts it by one. Could our program do the same?

4From counting again to sweeping once

If we visit the events in time order, the count after each event is just the previous count plus one for an arrival or minus one for a departure. That is the clicker. So the only thing we need is the list of all events in time order.

Here comes the step that surprises beginners. Do we need to keep each arrival paired with its own departure? Let us think about the clicker again. It does not know which guest is leaving, only that someone is. The count at any moment depends only on how many arrivals and how many departures have happened so far. So we can sort the arrival times on their own and the departure times on their own, and forget which train each time belonged to. The sorted arrivals are 1000, 1010, 1025, 1100, 1105, 1300 and the sorted departures are 1020, 1030, 1110, 1115, 1200, 1310. Notice that 1020 now comes first, although it belonged to the second train.

Now we sweep both sorted lists with two pointers, i into the arrivals and j into the departures. At each step the next event in time is the earlier of arr[i] and dep[j]. If arr[i] <= dep[j], a train arrives: the count goes up, we update the most seen, and i moves on. Otherwise a train leaves: the count goes down and j moves on. We can stop when the arrivals run out, because after that the count can only fall.

The tie rule lives in that one comparison. With <=, when an arrival and a departure happen in the same minute, the arrival is counted first, so both trains are present together, matching our assumption. If the station allowed a train to use a platform in the same minute another leaves, we would write < so the departure is processed first. A tempting mistake is to write < without thinking about ties, which quietly gives an answer one too small.

PictureTrains in the station after each event

One bar per event, in time order. An arrival makes the next bar one taller and a departure makes it one shorter. The tallest bar is the answer.

  1. 12121232101peak
    events
    A A D A D A A D D D A
    peak
    3

    1The count only ever changes by one at a time. It climbs to 3 at the arrival at 1105, and that peak is the number of platforms.

The bar chart shows the clicker's reading after each of the eleven events the loop takes. Seeing it this way, the whole problem becomes finding the tallest bar, and each bar is the one before it plus or minus one.

5Watching the sweep on the example

The picture plays the loop on the sorted times. The top row holds the sorted arrivals and the bottom row the sorted departures. In each frame the two times being compared are marked, the event just taken is highlighted, and times already handled are shown as finished. The panel gives the event, the two pointers, the count and the most so far.

Watch the comparison in every frame. Only one of i and j moves, and it is the one whose time came first.

Step throughSweeping through the sorted times

Top row: sorted arrivals. Bottom row: sorted departures. The two times being compared are marked, the event just taken is highlighted, and handled times are finished.

  1. 100010101025110011051300102010301110111512001310
    event
    start
    i, j
    0, 0
    in station
    0
    most
    0

    1Both lists are sorted. i points at the first arrival, 1000, and j at the first departure, 1020. The station is empty.

  2. 100010101025110011051300102010301110111512001310
    event
    arrival 1000
    i, j
    1, 0
    in station
    1
    most
    1

    21000 <= 1020, so the next event is an arrival. One train comes in, the count becomes 1, and the most so far is 1.

  3. 100010101025110011051300102010301110111512001310
    event
    arrival 1010
    i, j
    2, 0
    in station
    2
    most
    2

    31010 <= 1020, another arrival. Two trains are now in the station, so the most so far rises to 2.

  4. 100010101025110011051300102010301110111512001310
    event
    departure 1020
    i, j
    2, 1
    in station
    1
    most
    2

    41025 is later than 1020, so a departure comes first. One train leaves, the count drops to 1, and j moves on to 1030.

  5. 100010101025110011051300102010301110111512001310
    event
    arrival 1025
    i, j
    3, 1
    in station
    2
    most
    2

    51025 <= 1030, so a train arrives and the count is back to 2. The most so far stays 2.

  6. 100010101025110011051300102010301110111512001310
    event
    departure 1030
    i, j
    3, 2
    in station
    1
    most
    2

    61100 is later than 1030, so the train due out at 1030 leaves first. The count drops to 1.

  7. 100010101025110011051300102010301110111512001310
    event
    arrival 1100
    i, j
    4, 2
    in station
    2
    most
    2

    71100 <= 1110, an arrival. The count goes to 2 again, which only equals the best so far.

  8. 100010101025110011051300102010301110111512001310
    event
    arrival 1105
    i, j
    5, 2
    in station
    3
    most
    3

    81105 <= 1110, another arrival before anyone leaves. Three trains stand together, so the most so far becomes 3.

  9. 100010101025110011051300102010301110111512001310
    event
    departure 1110
    i, j
    5, 3
    in station
    2
    most
    3

    91300 is later than 1110, so the departure at 1110 is taken and the count falls to 2.

  10. 100010101025110011051300102010301110111512001310
    event
    departure 1115
    i, j
    5, 4
    in station
    1
    most
    3

    101300 is still later than the next departure, 1115, so another train leaves and the count is 1.

  11. 100010101025110011051300102010301110111512001310
    event
    departure 1200
    i, j
    5, 5
    in station
    0
    most
    3

    111300 is later than 1200 as well, so the third departure in a row empties the station. The count is 0.

  12. 100010101025110011051300102010301110111512001310
    event
    arrival 1300
    i, j
    6, 5
    in station
    1
    most
    3

    12Finally 1300 <= 1310, so the last train arrives and the count is 1. i has passed the last arrival, so the loop stops. The answer is 3.

Let us read a few frames back. In the fourth frame 1025 is compared with 1020, and the departure wins because it is earlier, even though the train that left at 1020 arrived second. That is the forgotten pairing at work, and it does not matter. In the eighth frame the arrival at 1105 comes before the departure at 1110, so three trains stand together and the most so far becomes 3.

The ninth to eleventh frames take three departures in a row, because 1300 is later than 1110, 1115 and 1200. The station empties completely before the last train arrives. After the arrival at 1300, i equals 6, the loop stops, and we return 3. The departure at 1310 is never looked at, which is fine, because it could only lower the count.

6The complete Java program

Here is the full class. main runs our example, two trains that never meet, two trains that touch at the same minute, and an empty schedule.

Java
import java.util.Arrays;
public class MinPlatforms {
// Sort arrivals and departures separately, then sweep through time in order.
// A train that arrives at the same minute another departs still needs its own platform.
static int minPlatforms(int[] arrive, int[] depart) {
int[] arr = arrive.clone(), dep = depart.clone(); // do not reorder the caller's arrays
Arrays.sort(arr);
Arrays.sort(dep);
int i = 0, j = 0, inStation = 0, most = 0;
while (i < arr.length) {
if (arr[i] <= dep[j]) { // next event is an arrival: one more platform busy
inStation++;
most = Math.max(most, inStation);
i++;
} else { // next event is a departure: a platform frees up
inStation--;
j++;
}
}
return most;
}
public static void main(String[] args) {
int[] arrive = {1000, 1010, 1025, 1100, 1105, 1300}; // times as hhmm on one day
int[] depart = {1030, 1020, 1110, 1115, 1200, 1310};
System.out.println(minPlatforms(arrive, depart)); // 3
System.out.println(minPlatforms(new int[] {900, 940}, new int[] {930, 1000})); // 1
System.out.println(minPlatforms(new int[] {900, 930}, new int[] {930, 1000})); // 2
System.out.println(minPlatforms(new int[] {}, new int[] {})); // 0
}
}

Let us read minPlatforms in the order it runs. First it clones both arrays. Sorting them separately breaks the pairing between arrive[i] and depart[i], and the caller may still need that pairing, so we sort copies instead of the caller's data. Then Arrays.sort puts each copy in increasing order.

The four variables start at zero. i reads the arrivals, j reads the departures, inStation is our clicker and most is the highest reading so far. The while loop runs as long as some arrival is still to come.

Inside the loop, the test arr[i] <= dep[j] asks whether the next arrival comes no later than the next departure. If so, a train comes in: we add one to inStation, raise most if needed with Math.max, and move i on. If not, the next departure is earlier, so a train leaves: we take one off inStation and move j on. We only update most on arrivals, because a departure can never create a new peak.

A beginner may worry that dep[j] could run past the end of its array. It cannot. We only take a departure when the next arrival is later than it, which means at least one train is in the station, so there is always a departure left to read. main prints 3 for the example, 1 for trains at 900 to 930 and 940 to 1000, 2 for trains that meet at 930, and 0 for an empty day.

7How much it costs, and the cases at the edges

Let us count on the example first. The sweep took eleven events, each a constant amount of work, so the sweep itself is O(n), never more than 2n steps. The two sorts cost more, about n log n comparisons each, so the whole method is O(n log n) time. For 30,000 trains that is roughly a million steps instead of 900 million.

The two sorted copies take O(n) space. If the caller does not need the pairing afterwards, we could sort the input arrays in place and use O(1) extra space beyond them.

Comparing the ways to count platforms
ApproachTimeExtra space
Count trains present at each arrival (brute force)O(n^2)O(1)
Sort by arrival, min-heap of departure timesO(n log n)O(n)
Sort both lists, sweep with two pointersO(n log n)O(n) for the copies

The edge cases are quiet. An empty schedule never enters the loop and returns 0. Trains that never overlap give 1. Trains that touch at one minute give 2 under our rule, and 1 if we change <= to <. If times can run past midnight, we first convert them to minutes from a fixed starting point, so that a later time is always a larger number, and then sweep as before.

8Follow-up questions

If times are whole minutes in one day, can we avoid sorting?

Yes. A day has 1,440 minutes, so we keep a count array of that size. For each train we add one at its arrival minute and subtract one at the minute just after its departure, which keeps the rule that a train is present during its departure minute. Then we walk the array from the start, keeping a running sum, which is the number of trains present in each minute, and the largest sum is the answer. That is O(n + 1440) time, linear in practice.

How would you also say which platform each train uses?

Then we need the pairing, so we sort the trains by arrival and keep a min-heap of busy platforms ordered by the time they free up, plus a pool of free platform numbers. For each arriving train we first release every platform whose train has already left, then give the train the smallest free number, or open a new platform if none is free. The number of platforms ever opened is the same answer as before, and every train now has its platform. It costs O(n log n).

9Conclusion

We found the minimum number of platforms by noticing it equals the largest number of trains present at once, and then counting the way a clicker at a door does. Because the count depends only on how many arrivals and departures have happened, we sorted the two lists separately and swept them with two pointers, adding one for each arrival and subtracting one for each departure, in O(n log n) time. The clue to remember is intervals competing for a shared resource: turn them into start and end events, sort, and sweep with a running count.

What to say in the interviewAbout 80 seconds aloud

Open with

Let me pin down the rules: train i arrives at arrive[i] and leaves at depart[i] on one day, and a train arriving in the same minute another leaves still needs its own platform.

Then, if they want more

The answer is the largest number of trains present at the same moment, because that many are needed then, and that many are always enough. The brute force stands at each arrival and counts the trains present, which is O(n squared), and each count starts from zero although consecutive counts differ by one train.

Since this is about overlapping intervals, I sweep through time. The count only depends on how many arrivals and departures have happened, not which train they belong to, so I sort the arrival times and the departure times separately and walk both with two pointers.

If the next arrival is less than or equal to the next departure, a train enters, so I add one and update the maximum; otherwise a train leaves and I subtract one. The less than or equal makes arrivals win ties, which matches the rule, and changing it to less than gives the other rule.

Sorting is O(n log n) and the sweep is O(n), with O(n) space for the sorted copies. An empty schedule returns zero.

About this answer

Last updated
15 November 2026
Written for
Java 21, using only java.util
Checked
The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
Where the question comes from
Public interview write-ups, reworded by Scrivio; counted 46 times in that collection. How it is counted
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Given train arrival and departure times, find the minimum number of platforms a station...": https://scrivio-interview-library.netlify.app/data-structures/solutions/given-train-arrival-and-departure-times-find-the-minimum-number-bc91ee/