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
Group Synonyms by Connectivity
00:00
5 left

Group Synonyms by Connectivity

MediumPython

Problem

Given a list of string pairs where each pair represents mutually synonymous words (aliases), return a grouped listing/mapping of all connected synonym components. For example, input: [["a", "b"], ["b", "c"], ["d", "e"]], output: {"a": ["a", "b", "c"], "d": ["d", "e"]}.

Asked in the Phone Screen stage. Coding question asked during phone screen for SDE under ASE.

Implement group_synonyms(pairs). Return a dictionary whose keys are the lexicographically smallest words in each component and whose values are lexicographically sorted component lists. Return {} for empty input.

Example: [["x", "y"], ["y", "z"]] returns { "x": ["x", "y", "z"] }.

Constraints

  • 0 <= pairs.length <= 1000
  • Each pair contains exactly two non-empty strings
  • Each word contains at most 100 characters
  • Words are case-sensitive
  • Pairs may repeat and may contain a word paired with itself

Function Signature

def group_synonyms(pairs):
Interviewer

Your question is Group Synonyms by Connectivity. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.