← 返回博客列表
Google

Google NG Round1 Coding Recap: Validate Tree from Parent Array

2026-04-14

Google NG Coding Cover

The input is a parent-array representation of a graph, and the task is to decide whether it is a valid tree. The coding is simple, but interview clarity matters.


Problem Restatement

Given an array parent of length n:

Return whether this structure is a valid tree.


Interview-Ready Wording

I validate the structure with three checks:

  1. Exactly one root
    Count how many indices satisfy parent[i] == -1. If it is not 1, return false.

  2. Every node is visited at most once from the root
    Build an adjacency list from parent to children, then run DFS from the root.
    Use a visited array. If a node is reached again, the structure is invalid, return false.

  3. Full coverage after DFS
    After traversal, if any node is still unvisited, it is disconnected, so return false.
    Only return true when all nodes are visited.


Complexity


Common Mistakes


One-Line Takeaway

This problem is about stating the full tree criteria clearly: one root, no repeated path access, and all nodes reachable.


If you need interview support, feel free to reach out anytime.

#googlevo #vo #newgrad #sde #jobsearch #interviewprep


Further Reading (External Links)


Need real interview questions? Contact WeChat Coding0201: Get Questions.

Contact

Email: [email protected]
Telegram: @OAVOProxy