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
Traverse a Graph With BFS/DFS
00:00
5 left

Traverse a Graph With BFS/DFS

EasyPython

Problem

Box may represent folder relationships, shared links, or shortcuts as a graph rather than a strict tree. Given an adjacency-list representation of this graph, return the folders visited by a breadth-first search starting from a specified folder.

Visit each folder at most once, even when cycles or multiple paths exist. When processing a folder, examine its neighbors in the order provided by the adjacency list. Ignore folders that are not reachable from start.

Formal Specification

Implement traverse_box_graph(graph, start), where graph is a dictionary mapping a folder ID string to a list of directly connected folder ID strings, and start is a folder ID string. Return a list of folder IDs in BFS visitation order. The input graph is unweighted, and every referenced folder appears as a key.

Examples

Example 1

Input: graph = {"A": ["B", "C"], "B": ["D"], "C": ["E"], "D": [], "E": []}, start = "A"

Output: ["A", "B", "C", "D", "E"]

B and C are visited before either of their children because BFS processes one level at a time.

Example 2

Input: graph = {"A": ["B"], "B": ["C", "A"], "C": []}, start = "A"

Output: ["A", "B", "C"]

The visited set prevents the edge from B back to A from causing repeated visits.

Constraints

  • 1 <= number of folders <= 10^5
  • 0 <= number of relationships <= 2 * 10^5
  • Folder IDs are non-empty strings
  • The graph may contain cycles, self-loops, and disconnected components
  • The starting folder exists in graph

Function Signature

def traverse_box_graph(graph, start):
Interviewer

Your question is Traverse a Graph With BFS/DFS. 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.