FastPrepDelete Nodes and Return an N-ary Forest
Problems/Delete Nodes and Return an N-ary Forest

Delete Nodes and Return an N-ary Forest

Google logoGoogle● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report from a Google ML-team phone interview describes deleting nodes from an N-ary tree. It is similar to the linked Delete Nodes and Return Forest problem, which uses a binary tree. The report does not say how the N-ary nodes are represented, the input limits, or the order of the results. Here, nodes are numbered, and each remaining tree is written out in breadth-first order. Our version is about 70% the same as the reported question.

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.
  • children describes a valid tree rooted at 0: every node except 0 appears in exactly one child list, no node is its own child, and all nodes are reachable from 0.
  • toDelete contains 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

Interview reports that mention this question

  • Google SWE III ML Experienced Loops: 10 Experiences (Part 3 of 4)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] deleteNaryForest(int[][] children, int[] toDelete) {
        // Write your code here.
    }
}
Checking account…