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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.