Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect Cycles in Resource Graph

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

Your question is Detect Cycles in Resource Graph. 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 directed graph representing resource allocation dependencies between microservices, determine whether the graph contains a cycle. The input consists of an integer n for the number of nodes labeled 0 to n - 1, and a list of directed edges edges where each edge [u, v] means service u is waiting on service v. Return True if any cycle exists, otherwise return False.

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(edges) <= 2 * 10^5
  • edges[i].length == 2
  • 0 <= u, v < n
  • Self-loops are allowed

Function Signature

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