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
Breadth-First Search
00:00
5 left

Breadth-First Search

EasyPython

Problem

Implement breadth-first search on a graph and return the order in which nodes are visited starting from a given node.

The graph is represented as an adjacency list: a dictionary where each key is a node and its value is a list of neighboring nodes. Visit neighbors in the order they appear in the list. If the start node is not in the graph, return an empty list.

Formal Specification

Write a function bfs(graph, start) that returns a list of nodes in BFS visitation order.

  • graph: dict mapping each node to a list of adjacent nodes
  • start: the starting node
  • Return: list of nodes visited in BFS order

Use a queue for traversal and avoid visiting the same node more than once.

Constraints

  • 0 <= len(graph) <= 10^4
  • Total number of edges across all adjacency lists is at most 10^5
  • Node values are hashable
  • The graph may contain cycles and disconnected components

Function Signature

def bfs(graph, start):
Interviewer

Your question is Breadth-First Search. 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.