Merge two named trees
I was asked this
No reports yet. Sign in to say where you were asked it.
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:
-
m["name"] = a["name"]andm["value"] = b["value"]. - Children are paired by name in order of appearance: the
j-th child ofanamedxpairs with thej-th child ofbnamedx, whenever both exist. Each pair is merged with these same rules. -
m["children"]listsa's children in their order. A paired child is replaced by its merged result; an unpaired child ofais copied with its whole subtree. - Then
b's unpaired children follow, in their order inb, 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
1to10^4nodes and depth up to500nodes. - 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 than2 * 10^4nodes.
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
t1ort2, 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, walka's children and pop the next same-named child ofbfor each, then appendb'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
t2with 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
t2only. - 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
