← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Google SWE interview with a graph theory problem that looked like a simple tree check but had more layers than I expected. One question, but it kept branching into follow-ups.

Questions Asked (1)

Q1

Given an undirected acyclic graph with n nodes and a list of edges, determine whether it forms a valid binary tree. If it does, return a node that can serve as the root.

Algorithms & Data Structures
Author's notes

I jumped straight to checking edge count and connectivity, which was fine, but I forgot that not every node is a valid root.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, validate that the graph is a valid binary tree by checking that it has exactly n-1 edges, is connected, and every node has at most 2 children. Then, identify the root as the node with no parent (in-degree 0) and verify that all other nodes have exactly one parent.

Pro tip: Clarify whether the graph is guaranteed to be acyclic and undirected; if not, you must also detect cycles. Mention that a binary tree can have at most one root, and if multiple nodes have in-degree 0, it's invalid.

1. Check edge count

Verify that the number of edges is exactly n-1. If not, it cannot be a tree.

2. Check connectivity and acyclicity

Perform a BFS/DFS from any node to ensure all nodes are reachable and no cycles exist (though acyclic is given, still verify connectivity).

3. Compute in-degrees

Calculate the in-degree (number of parents) for each node. In a valid binary tree, exactly one node has in-degree 0 (the root) and all others have in-degree 1.

4. Check binary constraint

Ensure every node has at most 2 children (out-degree ≤ 2). If any node has more than 2 children, it's not a binary tree.

5. Return root

If all checks pass, return the node with in-degree 0 as the root. Otherwise, indicate invalid.

Key Points to Mention

  • A tree with n nodes must have exactly n-1 edges.
  • The root is the unique node with in-degree 0.
  • Every non-root node must have exactly one parent (in-degree 1).
  • Each node can have at most 2 children (out-degree ≤ 2).
  • Connectivity ensures all nodes are part of the tree.
  • If the graph is not guaranteed acyclic, cycle detection is necessary.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.