PPractick

DSA interview question

Minimum Number of Platforms Required for a Railway Station

Medium · Algorithms · Topics: greedy, sorting

The problem

A station has a timetable of arrivals and departures, as HHMM integers. A train occupies a platform from its arrival until its departure, inclusive: a train arriving at the same minute another departs still needs its own platform. What's the minimum number of platforms so no train waits? That's the maximum number of trains present at any moment. Sort the arrivals and departures separately and sweep through time: each arrival takes a platform, each departure frees one, and an arrival is processed before a departure at the same time.

Example: arrivals [900, 940, 950, 1100, 1500, 1800], departures [910, 1200, 1120, 1130, 1900, 2000] → 3.

Function signature

int findPlatform(vector<int> arr, vector<int> dep);  // dep[i] >= arr[i]

Test cases

Input (arr, dep)OutputExplanation
([900, 940, 950, 1100, 1500, 1800], [910, 1200, 1120, 1130, 1900, 2000])311:00–11:20 has three trains
([900, 1100, 1235], [1000, 1200, 1240])1No overlaps
([1000, 1000, 1000], [1010, 1010, 1010])3
([900, 905], [905, 910])2Arriving as the other leaves still needs a platform
([100], [200])1

(Confirm: times are within one day; a train occupies its platform during [arrival, departure] inclusive.)

How interviewers phrase it

Clarifying questions to ask

  1. If a train arrives exactly when another departs, can they share a platform? (Not here: inclusive.)
  2. Are times within a single day? (Yes.)
  3. Is dep[i] ≥ arr[i] guaranteed? (Yes.)

Follow-ups to expect

  1. "Why can the two arrays be sorted independently?"
  2. "How do you handle equal times?"
  3. "Min-heap alternative?"
  4. "Sweep-line alternative?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in