Tree Height After Child Promotion
Updated
Problem statement
You are given a rooted tree in which a node may have any number of children and distinct non-root node IDs to delete. Deleting a node removes that node and attaches each of its current children directly to its parent. Apply every listed deletion and return the resulting tree height, measured as the number of nodes on the longest root-to-leaf path. The root remains, so a tree containing only the root has height 1.
For this exercise, nodes are labeled 1 through n, root 1, and parent[i - 2] is the parent of node i. Delete the node IDs in deleted. You may delete both an ancestor and one of its descendants. Keep attaching their children to the next parent as needed. The remaining parent-child relationships are the same regardless of deletion order.
Function
heightAfterPromotions(parent: int[], deleted: int[]) → intExamples
Example 1
parent = [1,1,1,2,2]deleted = [2]return = 2Deleting an internal node promotes both children.
Example 2
parent = [1,2,3]deleted = []return = 4An unchanged four-node chain has height four.
Example 3
parent = [1,2,2]deleted = [3]return = 3Deleting one leaf preserves the other deep path.
Constraints
1 <= n = parent.length + 1 <= 10000.- The parent array describes one valid tree rooted at
1; parent IDs need not be smaller than child IDs. deletedcontains distinct existing IDs from2throughn, possibly none or all non-root nodes.
More Snowflake Phone Screen questions
- Minimum StackPHONE SCREEN · Seen Nov 2026
- Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Distance Between Two Nodes in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Time to Burn a Binary Tree From One NodeAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions