Minimum Bus Routes to a Destination
Updated
Problem statement
Each row in routes lists the distinct stops served by one bus route. Boarding a route costs one bus; after boarding, you may travel to any stop on that route and transfer to another route serving the same stop.
Return the minimum number of buses needed to travel from sourceStop to targetStop. Return 0 when the two stops are equal and -1 when the destination is unreachable.
Function
minimumBusRoutes(routes: int[][], sourceStop: int, targetStop: int) → intExamples
Example 1
routes = [[1,2,7],[3,6,7]]sourceStop = 1targetStop = 6return = 2Take the first route to stop 7, then transfer to the second route and reach 6.
Example 2
routes = [[1,2,3],[4,5,6]]sourceStop = 1targetStop = 6return = -1The two routes share no stop, so no transfer connects the endpoints.
Example 3
routes = [[5,8],[8,9]]sourceStop = 5targetStop = 5return = 0No bus is needed when the source already equals the destination.
Constraints
1 <= routes.length <= 500.1 <= routes[i].length <= 100000.- The total number of route-stop entries is at most
200000. - Stops within one route are distinct integers in
[0,10^9]. 0 <= sourceStop,targetStop <= 10^9.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Fewest Buses with the Full RouteMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Minimum Bus Routes to a DestinationPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Jump Game IVZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Largest Island After One FlipOracle · ONSITE INTERVIEWOracle interview questions
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Word Ladder II: All Shortest TransformationsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Word Ladder IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Concurrent Web Crawler SimulationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Currency Conversion RateAmazon · PHONE SCREEN · ONSITE INTERVIEW