Code Room
CodingMediumcod-g924
Subject Cycle detectionLevel Mid–Senior~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

A directed graph has n nodes (labeled 0..n-1); graph[i] is the list of nodes i points to. A node is 'safe' if every possible path starting from it eventually reaches a terminal node (a node with no outgoing edges) — equivalently, no path from it can ever enter a cycle. Return all safe nodes in ascending order.

Implement
eventual_safe_nodes(graph: list[list[int]]) → list[int]
Examples
in[[[1,2],[2,3],[5],[0],[5],[],[]]]out[2,4,5,6]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.