← Questions

Merge two named trees

0
GoogleCodingLast asked May 2026

A coding interview question asked in Google's onsite coding round, last asked May 2026. It tests trees, recursion and hash maps.

Each tree node is a dictionary with three keys: name (a string), value (an integer) and children (an ordered list of nodes). Given two such trees t1 and t2, build a new merged tree that keeps t1's names and child order and takes its values from t2.

Interface

def merge_trees(t1: dict, t2: dict) -> dict:

Merging node a (from t1) with node b (from t2) gives node m:

  1. m["name"] = a["name"] and m["value"] = b["value"].
  2. Children are paired by name in order of appearance: the j-th child of a named x pairs with the j-th child of b named x, whenever both exist. Each pair is merged with these same rules.
  3. m["children"] lists a's children in their order. A paired child is replaced by its merged result; an unpaired child of a is copied with its whole subtree.
  4. Then b's unpaired children follow, in their order in b, each copied with its subtree.

The two roots are always merged with each other, even if their names differ. Every node in the result has exactly the keys name, value and children; a leaf has "children": [].

Example

t1 = root(1): [a(2), b(3)]
t2 = top(10): [b(30), c(40)]
->   root(10): [a(2), b(30), c(40)]
# root names differ but the roots still merge; a is t1-only; c is t2-only and goes last

t1 = r(0): [x(1): [y(2)], z(3), x(4)]
t2 = r(9): [x(5): [w(6), y(7)], x(8), x(11): [v(12)]]
->   r(9): [x(5): [y(7), w(6)], z(3), x(8), x(11): [v(12)]]
# the two x's of t1 pair with the first two x's of t2; the third x of t2 is appended
# inside the first pair, y pairs with y and the unpaired w is appended after it

(name(value): [children] is shorthand for the dictionaries.)

Constraints

  • Each tree has 1 to 10^4 nodes and depth up to 500 nodes.
  • Names are up to 20 lowercase letters and digits. Siblings may share a name.
  • Values are integers in [-10^9, 10^9]. The result has fewer than 2 * 10^4 nodes.

Clarifying questions

  • If the roots' names differ, do they still merge? (Yes here.)
  • How are siblings with the same name matched: by position among same-named siblings?
  • May the function modify t1 or t2, or must it copy?
  • Is recursion depth a concern?

What a strong answer covers

  • Recursion per node: index b's children by name into queues of positions, walk a's children and pop the next same-named child of b for each, then append b's children that were never popped.
  • Deep copying unpaired subtrees so the result shares nothing with the inputs.
  • O(n1 + n2) time; an explicit stack if depth were larger.
  • Tests: different root names, repeated sibling names with uneven counts on each side, a node present only in t2 with its own subtree.

Follow-ups

  • Match children by name only, ignoring order, with at most one child per name.
  • Report which nodes were taken from t2 only.
  • Do it iteratively.

Where it's asked

  • Google · Onsite coding · Last asked May 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

trees · recursion · hash maps

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding