Delete Selected Nodes from a Parent-Array Forest
Updated
Problem statement
You are given one or more rooted trees with node IDs 0 through n - 1. The array parent describes these trees. A root has parent -1; every other entry is its direct parent's node ID. You are also given distinct node IDs in deleted.
Remove only the nodes in deleted and every edge connected to them. Keep every other node. In particular, a node that remains becomes a root if its direct parent is removed; do not delete that node's descendants automatically and do not connect it to another ancestor.
Return a new array of length n in original node-ID order. Write -2 for a removed node, -1 for a surviving root, and the unchanged direct parent ID for every other surviving node. Treat all deletions as happening at the same time. Do not change either input.
Function
deleteForestNodes(parent: int[], deleted: int[]) → int[]Examples
Example 1
parent = [-1,0,0,1,1,-1,5]deleted = [1,5]return = [-1,-2,0,-1,-1,-2,-1]Nodes 1 and 5 disappear. Their surviving children become roots; the edge from node 0 to node 2 remains.
Example 2
parent = [-1,0,1,2]deleted = [1,2]return = [-1,-2,-2,-1]Both selected adjacent nodes disappear. Node 3 survives as a root; descendants are not deleted automatically.
Example 3
parent = [2,2,-1]deleted = []return = [2,2,-1]An empty deletion list leaves the forest unchanged, including parent IDs greater than child IDs.
Constraints
1 <= n = parent.length <= 2000.- The parent array describes a valid rooted forest with no cycles. Roots use
-1; every other parent is an existing different node ID. 0 <= deleted.length <= n; all selected IDs are distinct and in range.- Parent IDs need not precede their children. Output slots retain original IDs; the result is not compacted.
More Snowflake Phone Screen questions
- 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
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Flat Records to a Hierarchical TreeAddepar · PHONE SCREEN
- Flat Comments to a Nested TreeByteDance · PHONE SCREENByteDance interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Construct a Tree from Level-Order and Inorder TraversalsAmazon · PHONE SCREEN
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Find Nodes with Invalid Parent LevelsZip · ONSITE INTERVIEWZip interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions