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 Related Communities
00:00
5 left

Graph Traversal for Related Communities

MediumPython

Problem

Given community->follower and follower->community maps, return all communities related to an input community (sharing followers); BFS.

Asked in the code screen stage.

Implement a function that takes a starting community and two maps, then returns every community reachable by repeatedly moving from a community to its followers, and from each follower to the communities they follow. Exclude the input community from the result. Return each related community once, in any order.

Function Input

  • start_community: the community to begin from
  • community_to_followers: dict mapping community id to a list of follower ids
  • follower_to_communities: dict mapping follower id to a list of community ids

Output

  • A list of related community ids

Constraints

  • 1 <= number of communities + number of followers <= 10^5
  • A community may have zero or more followers
  • A follower may follow zero or more communities
  • The graph may contain cycles
  • Return each related community at most once

Function Signature

def related_communities(start_community, community_to_followers, follower_to_communities):
Interviewer

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