Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Tree or Graph Traversal

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

Your question is Tree or Graph Traversal. 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

EOG engineers model dependencies between well assets as a directed graph. Given an asset dependency graph and a starting asset, return the assets reachable from the start in breadth-first search order.

Each graph key identifies an asset, and its value is a list of directly dependent assets. Visit neighbors in the order they appear in each list. An asset must appear at most once, including when the graph contains cycles. If the starting asset has no outgoing edges, return a list containing only that asset.

Formal Specification

Implement traverse_assets(graph, start).

  • graph is a dictionary mapping strings to lists of strings.
  • start is a string identifying the starting asset and is guaranteed to exist in graph.
  • Return a list of strings containing every asset reachable from start, in BFS order.

Constraints

  • 1 <= len(graph) <= 10^5
  • The total number of listed edges is at most 2 * 10^5
  • Asset identifiers are non-empty strings
  • Every listed neighbor is a key in graph
  • start is a key in graph

Function Signature

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