Compress an n-ary tree by promoting children
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon, Microsoft and Meta. It tests trees, dfs and recursion.
Each node of an n-ary tree holds a unique integer value and an ordered list of children. You are given the root and a list compressed_nodes of values.
Implement compress(root, compressed_nodes) -> root. For every node whose value appears in the list, remove that node and promote its children: they take the removed node's place in its parent's child list, in their original order. The root is never compressed, even if its value is listed. Values that do not appear in the tree are ignored. A compressed node may have compressed descendants; their children move up as far as needed.
class Node:
val: int
children: list[Node]
Constraints
-
1 <= n <= 10^5nodes; the tree can be very deep (for example, a single path). -
0 <= len(compressed_nodes) <= n. - You may change the tree in place.
Example 1
Tree (each line is parent -> children):
10 -> [20, 30, 40]
20 -> [50, 60]
30 -> [70]
compress(root, [20]) returns:
10 -> [50, 60, 30, 40]
30 -> [70]
Node 20 is gone, and 50 and 60 take its place, before 30.
Example 2
Same starting tree, compress(root, [30, 70, 10]). Node 30 is removed, and so is its only child 70, a leaf, so nothing is promoted from that branch. 10 is the root, so it stays. Result:
10 -> [20, 40]
20 -> [50, 60]
Part 2: Return the height of the compressed tree (edges on the longest root-to-leaf path) without changing or rebuilding the tree.
Clarifying questions
- If a compressed node has compressed children, should their children be promoted all the way up into the nearest kept ancestor, in order?
- May I change the tree in place, or must I return a new tree?
- Can
compressed_nodescontain duplicates or values not in the tree? - In Part 2, is the height of a tree with only the root 0?
What a strong answer covers
- Puts
compressed_nodesin a set, then rebuilds each kept node's child list: a kept child stays, a compressed child is replaced by the result of expanding its own children the same way, in order. - Does this without recursion (an explicit stack, processing children before their parent, or a stack of child iterators), so a path of 10^5 nodes doesn't overflow.
- Runs in O(n) time, since each node is visited once and each child list is rebuilt once.
- Part 2 computes the height on the original tree with an iterative traversal where a compressed node adds no depth, and a compressed node with no kept descendants adds no path at all.
- Tests: both examples, an empty list, compressing every node but the root, a chain of compressed nodes, a compressed leaf, and the root in the list.
Follow-ups
- Return the compressed tree as a new tree, leaving the input unchanged.
- Undo the compression later.
- Compress every node with exactly one child.
- Serialize the compressed tree to a string and back.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.


