Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Topological Sort for Dependencies

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Topological Sort for Dependencies. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output