I jumped straight to cycle detection and forgot to check for multiple roots first.
Approach this problem by identifying the necessary and sufficient conditions for a valid rooted tree: exactly one root (one -1), no cycles, and all nodes connected (i.e., the graph is fully connected). Use either Union-Find or DFS/BFS to detect cycles and verify connectivity, while simultaneously validating structural constraints like a single root and each node having at most one parent.
Pro tip: Mention edge cases upfront — such as an empty array, multiple roots, self-loops (parent[i] == i), or nodes pointing to out-of-range indices — as Google interviewers specifically look for candidates who think defensively and don't just solve the happy path.
Confirm input assumptions: can the array be empty, can indices be out of bounds, and is exactly one -1 guaranteed? Explicitly state the edge cases you plan to handle, such as multiple roots, self-loops, or disconnected components.
Articulate that a valid rooted tree requires: (1) exactly one node with parent -1, (2) no cycles, and (3) all n nodes are connected forming a single component. This gives your solution a clear checklist to verify.
Select Union-Find for O(n α(n)) efficiency or DFS/BFS for clarity, and justify your choice. For Union-Find, union each node with its parent and detect a cycle if they already share the same root; for DFS, track visited and in-recursion-stack states.
Code the solution while narrating your logic — count roots as you scan, apply your cycle-detection strategy, and after processing all edges verify that the number of connected components equals exactly one.
State time complexity O(n) or O(n α(n)) and space complexity O(n), then walk through 2-3 test cases: a valid tree, a graph with a cycle, and a disconnected forest to confirm correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.