Fewest Shuttle Rides Between Two Stops on an Amazon Campus

Last updated: November 29, 2026

Quick Overview

Each campus shuttle loops forever through a fixed list of stops. Given all shuttle routes, a source stop and a destination stop, return the minimum number of shuttles you must ride to get from source to destination, or -1 if impossible.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Each campus shuttle loops forever through a fixed list of stops. Given all shuttle routes, a source stop and a destination stop, return the minimum number of shuttles you must ride to get from source to destination, or -1 if impossible.

An Amazon onsite hard BFS question about getting between buildings on a large corporate campus. The interviewer checks that you BFS over routes (shuttles) rather than stops, since the cost is the number of boardings, and that you mark routes and stops visited to keep the search linear.

What the Interviewer Expects
  • Notice the cost is shuttles boarded, not stops passed
  • Build a map from stop to the routes serving it
  • BFS over routes, level by level
  • Mark both routes and stops visited to avoid rescanning
  • Return 0 when source equals destination
Key Topics to Cover
BFS
Graph Modeling
Hash Map
Shortest Path
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if each ride also has a travel time and you minimize time?
  • How would you return the actual sequence of shuttles?
  • What if some shuttles only run in one direction during certain hours?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: routes, list of stop-ID lists (each route visits its stops cyclically); source, target stop IDs. Output: minimum number of routes boarded, or -1. Up to 500 routes and 1e5 total stops....

High-Level Design

Build stop_to_routes. Start by boarding every route that serves the source, which costs 1. BFS over routes: when processing a route at distance d, every stop on it is reachable with d boardings; if ...


Submit Your Answer
Markdown supported