Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Graph Traversal Implementation
00:00
5 left

Graph Traversal Implementation

EasyPython

Problem

Guardian Life models dependencies between internal services as a directed graph. Given a starting service and a destination service, determine whether the destination is reachable by following one-way dependency edges.

Implement can_reach(graph, start, target) using breadth-first search or depth-first search. The graph is represented as an adjacency list, where each key is a service name and its value is a list of directly dependent services. Return true if target can be reached from start, including when both names are the same. Return false when no path exists.

Formal Specification

  • Input: graph, a dictionary mapping strings to lists of strings; start and target, service-name strings.
  • Output: A boolean indicating whether a directed path exists from start to target.
  • The graph may contain cycles, self-loops, disconnected services, and services that appear only as neighbors.

Constraints

  • 1 <= number of services <= 10^5
  • 0 <= number of directed edges <= 2 * 10^5
  • Service names are non-empty strings
  • The graph may contain cycles, self-loops, disconnected services, and repeated edges

Function Signature

def can_reach(graph, start, target):
Interviewer

Your question is Graph Traversal Implementation. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.