Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect Circular Dependencies

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

Your question is Detect Circular 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

IDFC FIRST Bank operates microservices whose dependencies form a directed graph. Given each service and the services it calls, detect whether any circular dependency exists and return one cycle if found.

Formal Specification

Implement detect_circular_dependencies(dependencies), where dependencies is a dictionary mapping a service name to a list of services it directly depends on. Return a list of service names representing any directed cycle, without repeating the first service. Return an empty list when the dependency graph is acyclic.

All service names are non-empty strings. A dependency may appear only in a list and should still be treated as a graph vertex with no outgoing dependencies. The order of a valid cycle is not important, and any one cycle is acceptable.

Constraints

  • 1 <= number of distinct services <= 10^5
  • 0 <= number of dependency edges <= 2 * 10^5
  • Service names are non-empty strings
  • The graph may be disconnected
  • A self-dependency counts as a cycle

Function Signature

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