Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

BFS for System Dependency Graph

Medium
CodingQueueTreesGraphsAsked 1 times

Problem

At Meta, services such as News Feed, Ads Delivery, and Graph API can depend on other internal systems. Given a directed graph of system dependencies and a starting system, implement breadth-first search (BFS) to return the order in which systems are discovered, grouped by dependency distance.

A directed edge A -> B means system A depends directly on system B. Starting from start, traverse all reachable systems using BFS. Return two results:

  1. The flat BFS traversal order.
  2. A list of levels, where each level contains systems at the same distance from start.

Input / Output

  • Input:
    • graph: a dictionary mapping each system name (string) to a list of directly dependent systems (list of strings)
    • start: a string representing the starting system
  • Output:
    • A dictionary with keys:
      • order: list of strings in BFS visitation order
      • levels: list of lists of strings, grouped by BFS depth

Constraints

  • 1 <= len(graph) <= 10^4
  • 0 <= total number of edges <= 10^5
  • System names are non-empty strings
  • The graph may contain cycles
  • start is guaranteed to be a key in graph

Function Signature

def bfs_dependencies(graph, start):

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Meta Solutions Architect Interview Questions
Next questions
NiCEService Dependency Graph TraversalMediumDepth-First Traversal of GraphEasyeBayBFS for Service ReachabilityMedium
Python 3.10