Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect and Resolve Retain Cycles

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

Your question is Detect and Resolve Retain Cycles. 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

Write a function to detect and resolve retain cycles in a provided block of asynchronous code.

Model retained references as a directed graph, where each edge means the source strongly retains the target. Return the directed edges that should be made weak to remove all cycles, using depth-first traversal order and preserving the supplied node and edge order. The function receives nodes and edges, and returns a list of [source, target] edges identified as DFS back-edges. Each node name is unique, and every edge references a listed node.

Constraints

  • 1 <= nodes.length <= 500
  • 0 <= edges.length <= 2,000
  • Each node name is a unique string
  • Every edge is a two-element list [source, target]
  • Every source and target appears in nodes
  • The input node and edge order must determine the output order

Function Signature

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