Your question is Detect Cycles in Routing 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.
Visa payment routing paths form a directed graph. Each node represents a routing participant or processing stage, and an edge u -> v means a payment may be forwarded from u to v. Implement a function that determines whether any routing path contains a directed cycle.
A cycle exists if a path can start at a node and follow directed edges back to that same node. The graph may be disconnected, may contain self-loops, and may reference a node that has no outgoing edges.
Implement detect_cycle(graph), where graph is a dictionary mapping each node identifier to a list of directly reachable node identifiers. Node identifiers are strings. Return True if the graph contains at least one directed cycle; otherwise, return False.
The solution must handle graphs with up to 100,000 nodes and edges without relying on Python's recursive call stack.
def detect_cycle(graph):