Fewest Shuttle Lines to Ride Between Two Campus Stops

Last updated: November 29, 2026

Quick Overview

Each shuttle line loops forever through a list of stops. Given all lines, a source stop, and a target stop, return the fewest lines you must board to travel from source to target, or -1.

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

0

0

0 solved


Each shuttle line loops forever through a list of stops. Given all lines, a source stop, and a target stop, return the fewest lines you must board to travel from source to target, or -1.

This bus-routes problem appears in the style of Meta's hard onsite questions, set on a large corporate campus shuttle network. The interviewer checks whether you pick the right graph: BFS over lines instead of stops, which avoids building a dense stop-to-stop graph.

What the Interviewer Expects
  • Notices that the cost is lines boarded, not stops visited
  • Maps each stop to the lines serving it
  • Runs BFS where each level is one more line boarded
  • Marks both lines and stops visited to avoid rescanning
  • Handles source equal to target with 0
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
  • How would you return the actual sequence of lines?
  • What if each line has a different fare and you want the cheapest trip?
  • How would you precompute answers for all stop pairs?
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: lines, a list of stop lists, plus source and target. Output: minimum number of lines boarded. Up to 500 lines with up to 10^5 total stop entries. Riding along a line is free; transfers co...

High-Level Design

Build stop -> lines through it. BFS where the frontier is a set of lines. Start with every line through source at depth 1. When you expand a line, scan its stops: if one is the target, return the ...


Submit Your Answer
Markdown supported