Count Visited Nodes in a Directed Graph
A hard-tier problem at 29% community acceptance, tagged with Dynamic Programming, Graph, Memoization. Reported in interviews at BNY Mellon and 0 others.
Count Visited Nodes in a Directed Graph is a hard-rated problem that asks you to traverse a directed graph and count reachable nodes from a starting point. With a 29% acceptance rate, it's a genuine checkpoint problem, not a warmup. BNY Mellon has tested it. The trap is that naive traversal will TLE on cyclic graphs, and you need to recognize when you've entered a cycle to avoid infinite loops. If this hits your live assessment and you blank on the cycle-detection trick, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Count Visited Nodes in a Directed Graph"
Count Visited Nodes in a Directed Graph 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. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe obvious approach, DFS or BFS from every node, fails because you don't know the graph structure upfront and can't tell if you're about to loop forever. The real pattern combines memoization with cycle detection: track nodes currently in your recursion stack (the path you're on right now), and once you hit a node that's already on your path, you've found a cycle. From that point, every node in the cycle and every node reachable from it gets counted exactly once. Dynamic programming stores results so you don't recompute subtrees. The insight is that once you know a node leads to a cycle of size K, any node pointing to it also counts K nodes. StealthCoder is the hedge if you haven't drilled this pattern.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Visited Nodes in a Directed Graph recycles across companies for a reason. It's hard-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. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Visited Nodes in a Directed Graph interview FAQ
Is this really asked at companies in practice?+
BNY Mellon has reported it. Hard-rated graph problems are less frequent in live interviews than medium ones, but they do appear. The 29% acceptance rate confirms it's a tricky problem, not a gotcha, just genuinely hard.
What's the trick I'm missing if my solution times out?+
You're likely revisiting nodes or stuck in infinite recursion on cycles. You need to track which nodes are currently in your recursion stack (on the current path) versus already fully processed. Once you hit a node on your current path, you've found a cycle and can stop.
How do Dynamic Programming and Memoization fit here?+
Once you compute how many nodes are reachable from node X, store that result. If another node points to X, you reuse that cached count instead of retraversing. This cuts redundant work dramatically and prevents timeouts on large graphs.
Can I just use a simple DFS with a visited set?+
Not safely. A single visited set across all DFS calls will incorrectly count nodes. You need to distinguish between nodes visited on the current recursion path (which signal a cycle) and nodes already fully explored in prior calls.
What if the graph has multiple disconnected components?+
The algorithm handles it naturally. You process each node independently. If a node is isolated or part of a small component, its count stays local. Cycles are detected per-component, and memoization works across all of them.
Want the actual problem statement? View "Count Visited Nodes in a Directed Graph" on LeetCode →