Reported February 2026
Googletree

Validate a Tree From Its Parent Array

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The edge case that breaks the naive answer on this Google OA, reported February 2026, is the lone cycle sitting next to a valid root. Count one -1 and return true, and you fail Example 2. The task is simple on paper: given a parent array, decide if it describes exactly one rooted tree. It's a tree problem in disguise, with a graph check underneath. If you've got an invite in your inbox, expect hidden tests on self-parents, disconnected cycles, and two roots. StealthCoder is the safety net running invisibly during the live OA if your mind goes blank on the cycle check.

The problem

You are given a parent array parent describing a directed parent relationship over nodes numbered from 0 through parent.length - 1.
parent[i] = -1 means node i is a root.
Otherwise, parent[i] is the index of node i's parent.
Return true if the array describes exactly one valid rooted tree, and return false otherwise.
A valid rooted tree has exactly one root, contains no directed cycle, and makes every node reachable from that root.

Function
isValidTree(parent: int[]) → boolean

Examples
Example 1
parent = [-1,0,0,1,1]
return = true
Node 0 is the unique root. Every other node is reachable from it, and no parent edge creates a cycle.
Example 2
parent = [-1,2,1]
return = false
Nodes 1 and 2 form a directed cycle and are not reachable from the root.
Example 3
parent = [-1,-1,0]
return = false
Nodes 0 and 1 are both roots, so the structure is not one rooted tree.

Constraints
1 <= parent.length <= 2 * 10^5.
Every parent[i] is either -1 or an integer from 0 through parent.length - 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Each node has at most one parent by construction, so you don't need to check for multiple parents. You need three things: exactly one -1, no cycles, and full reachability. Count roots first. If it isn't exactly one, return false. Then build children lists and run a BFS or iterative DFS from the root, counting visited nodes. If visited equals n, every node is reachable, and that rules out cycles too, since a cycle component can't be reached from the root. Pitfall one: recursive DFS on 2 * 10^5 nodes blows the stack on a chain, so go iterative. Pitfall two: parent[i] == i is a self-loop, and the reachability count catches it. Pitfall three: counting roots alone, which misses the cycle case. The whole thing is O(n) time and O(n) space. If you freeze on the traversal, StealthCoder can hand you the working version live.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Validate a Tree From Its Parent Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Validate a Tree From Its Parent Array FAQ

What's the trick in Validate a Tree From Its Parent Array?+

Count the roots, then traverse from the root and count visited nodes. If there's exactly one -1 and the traversal reaches all n nodes, it's a valid tree. Reachability from the root also rules out any cycle, so you don't need a separate cycle detector.

How hard is this Google OA question really?+

Easy to medium. The algorithm is a single traversal. The difficulty is the edge cases: a cycle that's disconnected from the root, zero roots, multiple roots, and self-parents. Candidates who only count roots get Example 2 wrong.

Should I use recursion or iteration?+

Iteration. With n up to 2 * 10^5, a parent array shaped like a long chain gives a recursion depth that can overflow the stack in many languages. Use a queue for BFS or an explicit stack for DFS, and you avoid the problem.

Can I solve it without building a children list?+

Yes. You can walk up from each node with a state array (unvisited, visiting, done) and detect cycles that way. You'd still need to confirm there's exactly one root. The children-list traversal is usually simpler to write correctly under pressure.

How do I prepare for this in 48 hours?+

Write the root count plus BFS version from scratch twice. Then test it by hand on three inputs: [-1,0,0,1,1], [-1,2,1], and [-1,-1,0]. Add a self-parent case like [0]. If you can explain why reachability implies no cycle, you're ready.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it