Bus Routes
Updated
Problem statement
A city has some buses. routes[i] lists the stops that bus i visits. Each bus drives its route in a loop forever: after the last stop in its list, it goes back to the first one. So while you are on a bus, you can ride from any of its stops to any other of its stops.
You start at stop source and you are not on a bus yet. You want to reach stop target. You can only move between stops by bus. You can get off at any stop of the bus you are on and get on any other bus that stops there.
Return the smallest number of buses you must take to reach target. If you start at the target (source == target), return 0. If target cannot be reached, return -1.
Function
numBusesToDestination(routes: int[][], source: int, target: int) → intExamples
Example 1
routes = [[4,9,2],[2,8,5],[5,11]]source = 4target = 11return = 3Take bus 0 from stop 4 to stop 2. Change to bus 1 and ride to stop 5. Change to bus 2 and ride to stop 11. That is 3 buses.
No shorter trip exists: stop 11 is only on bus 2, stop 4 is only on bus 0, and these two buses share no stop, so you need a bus in between.
Example 2
routes = [[10,3,6,1],[6,7]]source = 1target = 10return = 1Stops 1 and 10 are both on bus 0. The bus runs in a loop, so after stop 1 it goes back to stop 10. You need 1 bus.
Example 3
routes = [[1,5],[6,9]]source = 5target = 9return = -1From stop 5 you can only take bus 0, which reaches stops 1 and 5. Bus 0 shares no stop with bus 1, so stop 9 cannot be reached. Return -1.
Example 4
routes = [[2,3]]source = 8target = 8return = 0You are already at the target, so you need 0 buses. It does not matter that no bus stops at stop 8.
Constraints
1 <= routes.length <= 5001 <= routes[i].length <= 10000- The total number of stops in all routes is at most
10000. 0 <= routes[i][j] <= 999999- The stops in one route are all different.
0 <= source, target <= 999999
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Shortest Path Through Numbers Sharing a DigitGoogle · PHONE SCREENGoogle interview questions
- Jump Game IVZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Largest Island After One FlipOracle · ONSITE INTERVIEWOracle interview questions
- Bus RoutesUiPath · PHONE SCREEN
- Minimum Bus Routes to a DestinationPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience