Clarify and Prune an N-ary Tree to a Depth Limit
Company: Snowflake
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
You are given a rooted N-ary tree and a nonnegative integer `k`. First compute the tree's maximum depth. Then discuss how to delete a minimum set of nodes so the resulting tree has maximum depth at most `k`.
The phrase “delete a node” is intentionally ambiguous. Before proposing an algorithm, clarify the depth convention, whether the root may be deleted, what happens to a deleted node's descendants, what “minimum” counts, and whether nodes already within the allowed prefix must be preserved. Do not assume that two different deletion contracts have the same optimum.
### Constraints & Assumptions
- The input is a valid rooted tree with no cycles or shared children.
- The tree may be empty unless the interviewer rules that out.
- An iterative traversal should be available for trees too deep for recursion.
- The interviewer requests non-DP reasoning, so exploit the structure implied by each clarified contract.
### Clarifying Questions to Ask
- Is root depth measured as zero or one?
- Does deleting a node remove its entire subtree, or are its children promoted to its parent?
- Is cost the number of removed original nodes or the number of explicit delete operations?
- Must every node at depth at most `k` be retained?
- May the root be deleted, and what should be returned when `k` is below the root's depth?
### Part 1 - Compute Maximum Depth
Give an iterative or recursive algorithm that returns the maximum depth under a stated convention. Explain the empty-tree result and the complexity.
#### What This Part Should Cover
- States whether root depth is zero or one, visits every node once, and avoids hidden assumptions about a binary-tree shape.
### Part 2 - Compare Deletion Interpretations
Analyze at least these two reasonable contracts:
1. **Minimum removed nodes with prefix preservation:** deleting a selected node removes its whole subtree, the root remains, and every original node at depth at most `k` must remain.
2. **Minimum subtree-cut operations without prefix preservation:** deleting a selected non-root node removes its whole subtree, each selected subtree root costs one operation, and it is legal to sacrifice nodes whose depths were already within the limit.
For each contract, characterize what an optimal result looks like. Also explain why allowing root deletion or promoting children would materially change the problem.
#### What This Part Should Cover
- Separates removed-node cost from operation cost, identifies degenerate cases, and does not present one interpretation as the reported interview's confirmed rule.
### Part 3 - Give Non-DP Algorithms
For both contracts above, provide a traversal-based algorithm, an optimality argument, output format, and time and space complexity. The output may include both the selected cut roots and the total number of original nodes removed so the two cost definitions remain visible.
#### What This Part Should Cover
- Uses depth or subtree-height information directly, explains why dynamic programming is unnecessary under the stated contracts, and handles `k`, empty-tree, and root-only edge cases.
### What a Strong Answer Covers
- Clarifies semantics before coding and maintains one depth convention throughout.
- Computes N-ary tree depth in linear time.
- Gives separate, correct optima for at least two deletion-cost models.
- Proves necessity and sufficiency instead of relying on an unexplained greedy rule.
- Calls out when a changed deletion model would require a different algorithm.
### Follow-up Questions
1. How would the answer change if deleting a node promoted its children to the deleted node's parent?
2. How would weighted deletion costs affect the non-DP conclusions?
3. How could maximum depth be maintained while leaves are inserted and removed online?
Quick Answer: Clarify an ambiguous N-ary tree task involving maximum depth and minimum deletion under a depth limit. Compare distinct deletion-cost contracts, preserve a consistent depth convention, handle empty and root-only trees, and justify correctness and complexity without assuming one interpretation.