Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Breadth-First Search in Practice

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

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

Amaris Consulting engineers model connected project components as a directed graph. Implement breadth-first search to visit every node reachable from a specified starting component, processing neighbors in the order provided.

Return the list of visited node names in BFS order. Each node must appear at most once, even when the graph contains cycles or multiple paths to the same node.

Formal Specification

Implement bfs_traversal(graph, start), where graph is a dictionary mapping a node name to a list of its directly connected neighbor names, and start is a node name. Return a list of node names in breadth-first order. The input guarantees that start exists in graph.

Use a queue to process nodes level by level. Nodes that are not reachable from start must not appear in the result.

Constraints

  • 1 <= number of graph nodes <= 10^5
  • 0 <= number of directed edges <= 2 * 10^5
  • Node names are unique strings
  • The start node exists in graph
  • The graph may contain cycles and self-loops

Function Signature

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