Find the Minimum Bus Fleet with Station Constraints
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Minimize a bus fleet when trips have origin and destination stations, tracking when buses become available at the correct departure station.
Constraints
- 0 <= len(trips) <= 200000.
- Each trip is an integer array [departure, arrival, origin, destination].
- 0 <= departure < arrival <= 1000000000.
- Station identifiers are integers from 0 through 1000000000, inclusive.
- A trip may start and end at the same station.
- Trips may be unsorted, and identical records still represent distinct trips.
- A bus arriving at the exact departure time is available for that departure at the same station.
- Buses may be placed at any stations before service begins, there is no turnaround time, and buses cannot move between stations except by serving a listed trip.
- Every listed trip must be served exactly once, and station identifiers have no distance or travel-time meaning.
- Return 0 for an empty schedule.
- Aim for O(n log n) time or better, where n is the number of trips.
Examples
Input: ([],)
Expected Output: 0
Explanation: An empty schedule needs no buses, so the answer is 0.
Input: ([[0, 1, 0, 0]],)
Expected Output: 1
Explanation: A single trip that starts and ends at station 0 still needs exactly one bus.
Hints
- The globally earliest finishing bus may be in the wrong place. Consider which availability information a departure from one particular station actually needs.
- Each trip contributes two separate moments to the day: the instant a bus must leave one station, and the instant a bus becomes free at another. Handling those moments in time order is enough to decide every trip.
- When two moments share a timestamp, the statement already fixes which one wins: a bus arriving at the exact departure time is available for that departure.