Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Cyclic Dependency

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

Your question is Find Cyclic Dependency. 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

Given a set of tuples, identify if there is a cyclic dependency.

Treat each tuple (a, b) as a directed dependency from a to b. Implement has_cyclic_dependency(dependencies), which returns True if any directed cycle exists and False otherwise. Nodes may appear only as dependency targets, and disconnected components must also be checked.

Examples: [("A", "B"), ("B", "C")] returns False; [("A", "B"), ("B", "A")] returns True.

Constraints

  • 0 <= len(dependencies) <= 1000
  • Each dependency is a pair of hashable node identifiers
  • Node identifiers may be strings or integers
  • A dependency tuple represents a directed edge from its first element to its second
  • Duplicate dependency tuples may occur

Function Signature

def has_cyclic_dependency(dependencies):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output