FastPrepMinimum Bus Routes to a Destination
Problems/Minimum Bus Routes to a Destination

Minimum Bus Routes to a Destination

Pinterest logoPinterest● HardFULLTIMEINTERNPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview reports identify minimum bus-route transfers. This practice version uses the recognizable minimum-buses contract and matches the visible source at about 92%.

Each row in routes lists all stops served by one bus route. Boarding a route costs one bus; after boarding, you may travel to any stop on it. Return the minimum buses needed to travel from sourceStop to targetStop, or -1 if impossible.

Function

minimumBusRoutes(routes: int[][], sourceStop: int, targetStop: int) → int

Examples

Example 1

routes = [[1,2,7],[3,6,7]]sourceStop = 1targetStop = 6return = 2

Take the first route to 7, then the second to 6.

Example 2

routes = [[1,5,7],[3,5]]sourceStop = 5targetStop = 5return = 0

The trip is already complete.

Example 3

routes = [[1,2],[3,4]]sourceStop = 1targetStop = 4return = -1

The route groups are disconnected.

Constraints

  • 1 <= routes.length <= 500.
  • The total number of listed stops is at most 10^5.
  • Stops on one route are unique.

More Pinterest problems

  • Gaussian Naive Bayes Classifier (Machine Learning OA)OA · Seen Nov 2026
  • Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
  • Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
All Pinterest interview questionsSee Pinterest hiring insights
Saved
class Solution {
    public int minimumBusRoutes(int[][] routes, int sourceStop, int targetStop) {
        // Write your solution here.
    }
}
Checking account…