Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
BFS Shortest Path Coding
00:00
5 left

BFS Shortest Path Coding

EasyPython

Problem

BNSF Railway models a simplified network of locations as an unweighted graph. Each edge represents a directly connected route segment, and every segment has equal cost. Given the graph, a starting location, and a destination, return the shortest path as an ordered list of locations.

If the destination cannot be reached, return an empty list. If the start and destination are the same, return a list containing that location.

Formal Specification

Implement shortest_route(graph, start, destination).

  • graph is a dictionary mapping each location name to a list of directly connected location names.
  • The graph is directed, so an edge from A to B does not imply an edge from B to A.
  • start and destination are strings present as keys in graph.
  • Return a list of strings containing the shortest path from start to destination, including both endpoints.
  • If multiple shortest paths exist, return any one of them.

Constraints

  • 1 <= number of locations <= 10^5
  • 0 <= number of edges <= 2 * 10^5
  • Location names are unique strings
  • The adjacency lists contain no duplicate edges
  • start and destination are keys in graph

Function Signature

def shortest_route(graph, start, destination):
Interviewer

Your question is BFS Shortest Path Coding. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.