Prune a Multiway Tree to a Maximum Depth
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
You are given a rooted multiway tree as a parent array. Node `0` is the root and `parent[i]` is the parent of node `i` for every `i > 0`. Root depth is `0`.
Deleting a node removes that node and its entire subtree. Return the minimum number of explicit node deletions needed so that every remaining node has depth at most `k`. The root may not be deleted.
### Function Contract
Implement `minimumSubtreeDeletionsForDepth(parent, k)` and return one integer.
### Constraints & Assumptions
- `1 <= len(parent) <= 200,000`.
- `parent[0] == -1` and `0 <= parent[i] < i` for `i > 0`, so the input is a valid rooted tree.
- `0 <= k <= len(parent)`.
- Deleting one node counts as one operation regardless of subtree size.
### Clarifying Questions to Ask
- Does deleting a node promote its children? No; it removes the whole subtree.
- Is root depth zero or one? Zero.
- May the root be deleted when `k` is negative? No; `k` is nonnegative.
```hint Cut each over-depth branch at its first illegal node
Every node at depth `k + 1` roots a disjoint subtree that must disappear. Deleting that node removes all deeper violations with one operation.
```
### Examples
```text
parent = [-1,0,0,1,1,3], k = 2 -> 1
```
Only node `5` is deeper than two; deleting node `5` is sufficient.
```text
parent = [-1,0,0,1,1,2,2], k = 1 -> 4
```
The four depth-two nodes are roots of four disjoint over-depth subtrees.
### Evaluation Focus
- Uses the stated depth convention and subtree-deletion semantics.
- Counts first illegal nodes, not every deeper descendant.
- Handles `k = 0`, a single-node tree, and `k` at least the existing maximum depth.
- Runs in `O(n)` time and `O(n)` or `O(height)` traversal space.
### Extensions to Discuss
1. How would weighted deletion costs change the greedy argument?
2. What if deletion promoted a node's children to its parent?
3. How would you return the exact nodes to delete?
Quick Answer: Given a rooted multiway tree as a parent array, return the minimum explicit subtree deletions needed to keep every remaining node within a maximum depth while preserving the root.
A rooted multiway tree is encoded by a parent array: node 0 is the root, parent[0] is -1, and parent[i] names an earlier parent for every i > 0. Root depth is zero. Deleting one node removes that node and its entire subtree, and the root cannot be deleted. Return the minimum explicit node deletions needed so every remaining node has depth at most k.
Constraints
- 1 <= len(parent) <= 200,000.
- parent[0] == -1 and 0 <= parent[i] < i for i > 0.
- 0 <= k <= len(parent).
- The root may not be deleted.
Examples
Input: ([-1,0,0,1,1,3], 2)
Expected Output: 1
Explanation: The only first-illegal node is node 5.
Input: ([-1,0,0,1,1,2,2], 1)
Expected Output: 4
Explanation: All four depth-two nodes require separate cuts.
Hints
- Only the first node that exceeds the limit on each branch needs to be cut.
- Parent-before-child order lets depths be computed in one forward pass.