Graph Valid Tree
A medium-tier problem at 49% community acceptance, tagged with Depth-First Search, Breadth-First Search, Union Find. Reported in interviews at Zenefits and 4 others.
Graph Valid Tree hits your OA and you're staring at n nodes, n-1 edges, asking if they form a valid tree. Companies like Google, Salesforce, LinkedIn, and Snowflake ask this constantly. The trap: you think "oh, it's just connected and acyclic" but the implementation matters. Most candidates either overcomplicate the cycle detection or miss the connectivity requirement entirely. If you freeze on the approach during the live assessment, StealthCoder surfaces a working solution in seconds, invisible to the proctor. With a 49% acceptance rate, this problem is easier than it looks once you know the pattern.
Companies that ask "Graph Valid Tree"
Graph Valid Tree 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 by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderA valid tree must have exactly n nodes, n-1 edges, be fully connected, and have zero cycles. The obvious DFS or BFS approach works: pick any node, traverse the whole graph, ensure you hit all n nodes and never revisit an edge. Union Find is faster if you think in terms of "connect nodes until you hit a conflict." The real gotcha is that checking "n-1 edges" alone is not enough. You need both connectivity and acyclicity. Most candidates code a basic cycle detector and forget to verify every node is reachable. The other trap: writing verbose DFS when a simple BFS with an adjacency list is cleaner. StealthCoder covers all three approaches and picks the cleanest one for your language.
Pattern tags
You know the problem.
Make sure you actually pass it.
Graph Valid Tree 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. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Graph Valid Tree interview FAQ
Is Graph Valid Tree still asked at FAANG companies?+
Yes. Google, Salesforce, LinkedIn, and Snowflake have reported it. It's a mid-level screener, popular because it tests both graph basics and reasoning about properties. Expect it to appear in online assessments and phone rounds.
What's the trick to avoid TLE on this problem?+
Don't do redundant checks. One pass with BFS/DFS to count reachable nodes and detect cycles is enough. Union Find is also O(n) with path compression. Avoid building the graph twice or running separate connectivity and cycle checks when one traversal does both.
How does this relate to Union Find vs DFS?+
Union Find is more elegant for cycle detection if you think of edges as union operations. DFS is more intuitive for graph traversal. Both are O(n). Pick based on comfort. Union Find fails faster if a cycle exists early. DFS is easier to code correctly under pressure.
What's the most common mistake candidates make?+
Checking n-1 edges and assuming the graph is a tree without verifying connectivity. A disconnected graph with two components and n-1 edges is not a tree. Always confirm all n nodes are reachable from any starting node.
Is this harder than BFS/DFS basics?+
Not harder, just requires combining two checks in one pass. If you're comfortable with standard DFS and cycle detection via parent tracking, you're ready. The 49% acceptance rate suggests it's medium-level but trips up people who overthink it.
Want the actual problem statement? View "Graph Valid Tree" on LeetCode →