Snowflake questions

Minimum Bus Fleet

SnowflakePhone screenMedium

A bus company operates a list of trips, where each trip is [departure, arrival, origin, destination]: a bus leaves station origin at time departure and reaches station destination at time arrival.

Buses may start the day at any station. A bus can only change stations by driving a trip, so after a trip it waits at that trip's destination. A bus that arrives at a station at time t can start a trip from that station departing at time t or later. Every trip is driven exactly once, by one bus.

Part 1: Minimum fleet size

You are given a list of trips as described above. Return the smallest number of buses that can drive all the trips. If trips is empty, return 0.

  1. Example 1
    trips
    [[0,4,1,2],[4,7,2,1],[4,6,1,2]]
    Output
    2

    Why: One bus drives the first trip, waits at station 2, and departs on the second trip at time 4. No bus is at station 1 when the third trip departs at time 4, so a second bus drives it.

  2. Example 2
    trips
    [[0,2,1,2],[3,5,1,2]]
    Output
    2

    Why: Both trips leave station 1. After the first trip the bus sits at station 2, where no trip departs, so the second trip needs its own bus.

Constraints

  • 0 <= len(trips) <= 2 * 10^5
  • 0 <= departure < arrival <= 10^9
  • 0 <= origin <= 10^9
  • 0 <= destination <= 10^9
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Minimum Bus Fleet: Snowflake interview question | LoopCoach