Pinterest interview question
Find the fewest buses needed to travel between two stops
- Company:
- Type:
- Coding
- Difficulty:
- Hard
- Reported:
- November 18, 2026
Problem
You are given an array routes, where routes[i] lists the stops of bus i. Each bus travels its route in a repeating loop forever, so you can ride bus i between any two of its stops. You start at stop source (not on a bus) and want to reach stop target, moving only by bus.
Return the least number of buses you must take to travel from source to target, or -1 if it is not possible.
Example 1
Input: routes = [[1, 2, 7], [3, 6, 7]], source = 1, target = 6
Output: 2
Take the first bus to stop 7, then the second bus to stop 6.
Example 2
Input: routes = [[7, 12], [4, 5, 15], [6], [15, 19], [9, 12, 13]], source = 15, target = 12
Output: -1
No sequence of buses connects stop 15 to stop 12.
Constraints
- Not stated; ask the interviewer.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern