← Snowflake Interview Insights
The deletion-promotes-children part is what gets you.
Model the problem as a tree DP where for each node you compute the minimum deletions needed to achieve each possible height, considering that deleting a node promotes its children. Then combine children's results and take the minimum across heights ≤ K at the root.
Pro tip: Clarify the deletion semantics early: deleting an interior node promotes its children to its parent, so the height reduction depends on the subtree structure, not just depth. Also mention that greedy pruning often fails because promoting children can increase height elsewhere.
Confirm that deleting a node removes it and promotes its children to its parent, preserving order, and that the root cannot be deleted. Discuss edge cases like K=0 (only root allowed) and already valid trees.
For each node u, define dp[u][h] = minimum deletions in subtree of u to make its height ≤ h, assuming u is not deleted. Also consider the option of deleting u, which merges its children into u's parent.
If u is kept, its height is 1 + max child height, so dp[u][h] = sum over children of min_{h' ≤ h-1} dp[child][h']. If u is deleted, its children become siblings of u's parent, so the cost is 1 + sum of dp[child][h] (same height allowance).
Process nodes in post-order. For each node, compute dp[u][h] for h from 0 to K (or up to original height). Use prefix minima over children's dp to optimize the sum.
The answer is dp[root][K] (root cannot be deleted). Time complexity is O(N * K) with prefix minima, space O(N * K). Mention that K can be up to N, so O(N^2) worst-case, but often acceptable.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.