Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Breadth-First Search Implementation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Breadth-First Search Implementation. Start with the requirements on the right.

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.

Problem

Unity tooling can model scene, prefab, and asset dependencies as a directed graph. Given an adjacency-list representation and a starting asset, return the nodes visited by a breadth-first search, preserving the neighbor order provided in the input.

Formal Specification

Implement bfs_graph(graph, start).

  • graph is a dictionary where each key is a node identifier and its value is a list of directly reachable node identifiers.
  • start is a node identifier. It may be omitted from graph, in which case it has no outgoing edges.
  • Return a list containing every node reachable from start, in BFS order.
  • Each node must appear at most once, even when the graph contains cycles or multiple paths to the same node.

Constraints

  • 1 <= number of graph nodes <= 10^5
  • 0 <= number of edges <= 2 * 10^5
  • Node identifiers are hashable and usable as dictionary keys
  • The graph may contain cycles and duplicate edges
  • The start node may not appear as a key in graph

Function Signature

def bfs_graph(graph, start):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output