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 on a Graph
00:00
5 left

BFS on a Graph

EasyPython

Problem

Virtual Vocations can model relationships between job-search resources as a graph. Given an unweighted graph and a starting resource, return the order in which a breadth-first search visits every reachable node.

The graph is represented as a dictionary. Each key is a node, and its value is a list of adjacent nodes in the required visit order. The graph may contain cycles, self-loops, nodes with no outgoing edges, and references to nodes that do not appear as dictionary keys. Visit each reachable node at most once. When multiple neighbors are available, process them in the order provided by their adjacency list.

Formal Specification

Implement bfs_traversal(graph, start).

  • Input: graph, a dictionary whose keys and adjacency-list values are node labels, and start, a node label.
  • Output: A list containing the labels reachable from start, in BFS order.
  • If start is not a key in graph, return [start], because the starting node is still considered visited.

Constraints

  • 1 <= number of reachable nodes <= 10^5
  • The graph is directed according to its adjacency lists
  • Node labels are hashable
  • Adjacency lists preserve the required neighbor visit order
  • A referenced node may be absent as a key from the graph

Function Signature

def bfs_traversal(graph, start):
Interviewer

Your question is BFS on a Graph. 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.