Delete Nodes and Return an N-ary Forest
Updated
Problem statement
An N-ary tree has nodes numbered 0 through n - 1, with root 0. children[i] lists the children of node i in their original left-to-right order. Delete every node whose number appears in toDelete. A deleted node is removed, but each of its surviving children becomes the root of a separate tree unless another deleted node breaks that tree further.
Return the remaining forest as a two-dimensional array. Each inner array lists the nodes of one surviving tree in breadth-first order, following the original child order and skipping deleted nodes. Order the inner arrays by their root node number, smallest first. If no nodes survive, return an empty array.
Function
deleteNaryForest(children: int[][], toDelete: int[]) → int[][]Examples
Example 1
children = [[1,2,3],[4],[5,6],[],[],[],[]]toDelete = [1,2]return = [[0,3],[4],[5],[6]]Deleting 1 and 2 splits their surviving children into roots 4, 5, and 6; node 0 stays connected to 3.
Example 2
children = [[1,2],[3],[],[]]toDelete = [0,2]return = [[1,3]]The original root and node 2 are removed. The remaining tree is rooted at 1 and contains 3.
Example 3
children = [[]]toDelete = []return = [[0]]The single root survives as a one-node tree.
Example 4
children = [[1,2],[],[]]toDelete = [0,1,2]return = []Every node is deleted, so the forest is empty.
Constraints
1 <= children.length <= 1000.childrendescribes a valid tree rooted at0: every node except0appears in exactly one child list, no node is its own child, and all nodes are reachable from0.toDeletecontains distinct node numbers in[0, n - 1]and may be empty.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions