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 for Interactions
00:00
5 left

Graph Traversal for Interactions

MediumPython

Problem

Coalition Technologies models relationships between users and items in an interaction graph. Given user-item interactions and a starting user, return every reachable user in breadth-first search order.

Treat each interaction [user, item] as an undirected edge between a user node and an item node. A user is reachable if a path from the starting user alternates through users and items. Return each reachable user once, including the starting user. The order must follow BFS discovery order, and neighbors must be processed in the same order their edges first appear in interactions.

Formal Specification

Implement connected_users(interactions, start_user).

  • interactions is a list of two-element lists of strings, where each pair contains a user ID and an item ID.
  • start_user is a string identifying a user present in interactions.
  • Return a list of user IDs in BFS discovery order. Item IDs must not appear in the result.
  • Repeated interactions do not create duplicate results.

Constraints

  • 1 <= len(interactions) <= 10^5
  • Each interaction contains exactly two non-empty strings
  • There are at most 2 * 10^5 distinct users and items
  • start_user appears as a user in interactions
  • User and item identifiers are unique within their respective categories

Function Signature

def connected_users(interactions, start_user):
Interviewer

Your question is Graph Traversal for Interactions. 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.