Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Breadth-First Search Coding

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

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

ACME's deployment planner represents service dependencies as a directed graph. Given an adjacency-list graph and a starting service, return the services reachable from that start in breadth-first order.

Visit each service at most once. When multiple services are available at the same depth, process them in the order they appear in the adjacency list. The graph may contain cycles, and services that cannot be reached from start must not appear in the result.

Formal Specification

Implement bfs_traversal(graph, start), where graph is a dictionary mapping strings to lists of neighboring service names, and start is a string. Return a list of strings containing the breadth-first traversal. Every service referenced as a neighbor is present as a graph key.

Constraints

  • 1 <= number of graph keys <= 10^5
  • The total number of directed edges is at most 2 * 10^5
  • Service names are non-empty strings
  • start is a key in graph
  • Every referenced neighbor is also a graph key
  • The graph may contain cycles and disconnected components

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