← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Google coding interview, one tree problem, not much else to go on but it was enough to make me second-guess my fundamentals for a week.

Questions Asked (1)

Q1

Given a list of binary tree nodes in shuffled order, find and return the root node.

Algorithms & Data Structures
Author's notes

My first instinct was to just look for the node that nobody points to as a child, which is the right idea, but I fumbled explaining it cleanly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the root is the only node that is never a child of another node. Use a hash set to track all child nodes, then find the node that is not in the set. Alternatively, compute the difference between the sum of all node values and the sum of all child values to find the root's value.

Pro tip: Mention that this approach works in O(n) time and O(n) space, and discuss trade-offs with other methods like using in-degree counts or XOR if node values are unique. Also, handle edge cases like a single node or empty list.

1. Understand the problem

Restate that the root is the only node not appearing as a child. Confirm assumptions about node structure and uniqueness of values.

2. Choose an approach

Select a method: hash set of children, sum difference, or XOR. Explain why it's efficient and correct.

3. Walk through the algorithm

Describe step-by-step: iterate through nodes, collect child references, then find the node not in the set. Use a simple example to illustrate.

4. Analyze complexity

State time and space complexity: O(n) time and O(n) space for hash set; O(1) space for sum difference if values are numeric.

5. Handle edge cases

Discuss empty list, single node, and potential duplicate values. Mention that if values are not unique, sum/XOR methods fail, so hash set is safer.

Key Points to Mention

  • Root is the only node that is never a child.
  • Use a hash set to track all child nodes for O(1) lookups.
  • Alternative: sum of all node values minus sum of child values equals root value.
  • Alternative: XOR of all node values XOR XOR of child values equals root value (if values unique).
  • Time complexity O(n), space complexity O(n) for hash set.
  • Edge cases: empty list, single node, duplicate values.

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