FastPrepParent-Child Deletion Order
Problems/Parent-Child Deletion Order

Parent-Child Deletion Order

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Google reports ask for an order that deletes every resource, where a resource can be deleted only after all of its children. One report's example is Example 4 with its letters numbered. That report adds a follow-up that deletes resources together when several are ready at once. The statement and editorial describe the follow-up, but it is not checked. The reports accept any valid order and give no input format or limits. Here, resources are numbered, the smallest ready resource goes first, and the limits are our choice. Our version is about 85% the same as the reported questions.

There are n resources numbered from 0 to n - 1. Each row [parent, child] in parentChild means that child must be deleted before parent.

The dependency graph is a directed acyclic graph. A resource may have more than one parent, and disconnected resources are allowed.

Return a valid deletion order containing every resource exactly once. When more than one resource can be deleted next, choose the resource with the smallest number. This way there is only one right answer.

Interview follow-up

Delete resources in batches instead. Each batch is every resource that has no remaining children once all earlier batches are deleted. Return the batches in order, with the numbers inside each batch in increasing order. The editorial covers this follow-up. Your function still returns the single deletion order described above.

Function

deletionOrder(n: int, parentChild: int[][]) → int[]

Examples

Example 1

n = 5parentChild = [[0,1],[0,2],[2,3],[2,4]]return = [1,3,4,2,0]

Resources 1, 3 and 4 have no children, so they are ready first. Always take the smallest ready resource: 1, then 3, then 4. Now resource 2 has no children left, so it goes next, and then resource 0. The order is [1,3,4,2,0].

Example 2

n = 4parentChild = [[0,2],[1,2],[1,3]]return = [2,0,3,1]

Resources 2 and 3 have no children, so the smallest, 2, goes first. That frees resource 0, whose only child was 2. Resource 1 still needs 3. Resources 0 and 3 are ready, so 0 goes next, then 3, which frees 1. The order is [2,0,3,1].

Example 3

n = 3parentChild = []return = [0,1,2]

No resource has a child, so all of them are ready at once. The smallest-number rule deletes them in increasing order: [0,1,2].

Example 4

n = 4parentChild = [[3,2],[3,1],[2,1]]return = [0,1,2,3]

Here parts D, C, B and A are numbered 0, 1, 2 and 3. Resource 3 holds 2 and 1, and 2 holds 1. Resources 0 and 1 start with no children, so 0 is deleted first and then 1. Deleting 1 frees 2, and deleting 2 frees 3. Starting with 1 would also be valid, but the smallest-number rule picks 0. The order is [0,1,2,3]. In the batch follow-up the batches are [0,1], [2] and [3].

Constraints

  • 1 <= n <= 100000
  • 0 <= parentChild.length <= 200000
  • Every relation has the form [parent, child].
  • 0 <= parent, child < n and parent != child.
  • The relation pairs are unique and form a directed acyclic graph.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Lexicographically Smallest Course OrderLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • ETL Pipeline SchedulerUnity · ONSITE INTERVIEW
  • Microservice Deployment OrderGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
  • Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Task Order with DependenciesNotion · ONSITE INTERVIEWNotion 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[] deletionOrder(int n, int[][] parentChild) {
        // Write your solution here
    }
}
Checking account…