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
Graph Traversal Coding Problem
00:00
5 left

Graph Traversal Coding Problem

MediumPython

Problem

Given a graph traversal type question, how would you approach solving it?

Implement breadth-first search for a directed graph represented by an adjacency-list dictionary. Starting from start, return the nodes in the order BFS visits them. Mark nodes when enqueued so cycles do not cause repeated visits. Preserve the neighbor order provided in each adjacency list.

Input: graph is a dictionary from node labels to lists of neighboring labels, and start is a node in graph. Output: a list of visited node labels. Assume every referenced neighbor appears as a key.

Examples: {"A":["B","C"],"B":["D"],"C":[],"D":[]}, "A" returns ["A","B","C","D"]; {"A":["B"],"B":["A"]}, "A" returns ["A","B"].

Constraints: up to 10^5 nodes and 2 * 10^5 directed edges; labels are hashable; the graph may contain cycles and disconnected components.

Constraints

  • 1 <= number of nodes <= 10^5
  • 0 <= number of directed edges <= 2 * 10^5
  • Every neighbor referenced in an adjacency list is a key in graph
  • Node labels are hashable
  • The graph may contain cycles and disconnected components

Function Signature

def bfs_traversal(graph, start):
Interviewer

Your question is Graph Traversal Coding Problem. 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.