Find the Minimum Fleet for a Bus Timetable
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Find the minimum number of buses needed for a timetable by tracking overlapping trips and allowing reuse at equal arrival and departure times.
Constraints
- 0 <= len(trips) <= 200000.
- 0 <= departure < arrival <= 1000000000 for every trip.
- A trip occupies the half-open interval [departure, arrival), so an arrival at the same time as another departure permits reuse.
- The input may be unsorted.
- Identical pairs represent distinct trips that must each be served.
- Return 0 for an empty schedule.
- Aim for O(n log n) time or better, where n is the number of trips.
- All values fit in a signed 32-bit integer; no value exceeds 2^31 - 1.
Examples
Input: ([],)
Expected Output: 0
Explanation: An empty schedule needs no buses, so the answer is 0.
Input: ([[0, 1]],)
Expected Output: 1
Explanation: A single trip always needs exactly one bus.
Hints
- The input order need not be the execution order. Consider what the schedule looks like when you examine departures and arrivals in time order instead.
- At each departure, ask whether any bus has already finished its previous trip. A bus that arrives at exactly time t is free for a trip departing at t, so equal timestamps need a deliberate tie rule.
- You are asked only for a count, not for an assignment, so you never have to record which bus serves which trip.