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
Topological Sort for Dependencies
00:00
5 left

Topological Sort for Dependencies

MediumPython

Problem

Samsung Ads services and campaign-processing components must be deployed only after their prerequisites. Given component names and directed dependency pairs, return a valid deployment order. If multiple components are available, choose the lexicographically smallest name to make deployments reproducible. Return an empty list when the dependencies contain a cycle.

Formal Specification

Implement resolve_dependencies(components, dependencies). components is a list of unique strings. Each item in dependencies is a two-element list [prerequisite, component], meaning prerequisite must appear earlier than component. Return a list containing every component exactly once, or [] if no complete ordering exists.

The result must be deterministic, including when several valid orders exist. Every dependency pair refers to names in components. Duplicate dependency pairs may appear and must not increase the required ordering more than once.

Constraints

  • 1 <= len(components) <= 10^5
  • 0 <= len(dependencies) <= 2 * 10^5
  • Component names are unique non-empty strings of length at most 50
  • Every dependency endpoint appears in components
  • Duplicate dependency pairs may occur

Function Signature

def resolve_dependencies(components, dependencies):
Interviewer

Your question is Topological Sort for Dependencies. 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.