Bus Routes
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given routes, where routes[i] lists the bus stops that bus i visits, repeating forever in a loop. You can ride a bus between any two stops on its route, and you can switch between buses at any stop they share.
You start at stop source (not on any bus yet) and want to reach stop target. Return the minimum number of **buses** you must board, or -1 if the target cannot be reached. If source == target, the answer is 0 even if that stop is on no route.
Aim for time roughly linear in the total number of stops listed across all routes.
Examples
Constraints
- 1 <= routes.length <= 500
- 1 <= routes[i].length <= 10^5 and the total of all routes[i].length <= 10^5
- The stops within one route are distinct
- 0 <= routes[i][j], source, target < 10^6
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.