Find Eventual Safe States
A medium-tier problem at 69% community acceptance, tagged with Depth-First Search, Breadth-First Search, Graph. Reported in interviews at Amazon and 0 others.
You're in a directed graph and need to identify which nodes can reach a terminal state without hitting a cycle. Find Eventual Safe States appears regularly in Amazon assessments and locks a lot of candidates because the cycle-detection trick isn't obvious on first read. The naive approach (depth-first search on every node) works but feels clunky. The real pattern is graph reversal or coloring: mark nodes that lead to cycles, then reverse the safety property. With a 68% acceptance rate, this problem rewards candidates who've seen the pattern before. If this one hits your live assessment and you blank on the approach, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Find Eventual Safe States"
Find Eventual Safe States is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe trap is treating this as a simple reachability problem. You can't just do DFS from each node to a terminal state because you'll waste time re-exploring subgraphs. The actual trick: a safe node is one that doesn't lead to any cycle and doesn't reach any cycling node. Use DFS with a three-state coloring system (unvisited, visiting, visited) or reverse the graph and do topological sort with in-degrees. Candidates who've drilled both Depth-First Search and Topological Sort together recognize this immediately. Those who haven't often get stuck implementing a slow brute force or misunderstanding what 'eventual' means. The reversal approach is elegant: if you reverse edges and compute in-degrees, only nodes with in-degree zero are safe. This is where StealthCoder becomes the hedge for the one algorithmic pattern you didn't drill.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find Eventual Safe States recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Eventual Safe States interview FAQ
Why doesn't a simple DFS from each node to terminal states work?+
Because you'll revisit the same subgraph repeatedly, killing your time complexity. More critically, you can't distinguish between nodes that reach a cycle and nodes that don't without careful state tracking. The coloring or reversal approach solves both in one pass.
Is Find Eventual Safe States still asked at Amazon?+
Yes. Amazon appears as the only top company in the data set for this problem, and the 68% acceptance rate suggests it's stable in their rotation. Treat it as a medium-priority graph drill if you have an Amazon assessment coming.
What's the difference between the DFS and topological sort approaches?+
DFS uses state coloring (unvisited/visiting/visited) to detect cycles in one pass. Topological sort reverses the graph and eliminates nodes with outgoing edges (in the original) to terminal nodes. Both are O(V + E) and both work. Pick whichever feels more natural when you code.
How does cycle detection fit into this problem?+
A node is unsafe if it leads to a cycle or to a node that leads to a cycle. Cycle detection via DFS coloring catches this directly. If you're at a node you've already marked 'visiting', you found a cycle. Mark all nodes in that cycle as unsafe.
Should I memorize both approaches or focus on one?+
If you're drilling Depth-First Search and Topological Sort separately, you'll recognize both patterns naturally during the OA. Reversing the graph and computing in-degrees is often faster to code under time pressure. Pick the one you've seen more in your problem set and drill that cold.
Want the actual problem statement? View "Find Eventual Safe States" on LeetCode →