Parent-Child Deletion Order
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The first attempt at this Google OA, reported in August 2026, usually fails on one thing: the edge direction. The pair is [parent, child], but the child gets deleted first, so you're running a topological sort on the reversed dependency. Get that backwards and every example breaks. The twist is the smallest-ID tie rule, which turns plain Kahn's algorithm into a heap-driven one. If you blank on the exact setup during the live assessment, StealthCoder sits invisibly on your screen as a safety net and hands you the structure.
The problem
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 tie rule makes the answer deterministic. Function deletionOrder(n: int, parentChild: int[][]) → int[] Examples Example 1 n = 5 parentChild = [[0,1],[0,2],[2,3],[2,4]] return = [1,3,4,2,0] Resources 1, 3, and 4 initially have no remaining children. Choosing the smallest available resource at each step yields [1,3,4,2,0]. Example 2 n = 4 parentChild = [[0,2],[1,2],[1,3]] return = [2,0,3,1] Deleting shared child 2 unlocks parent 0 but not parent 1, which still depends on child 3. The smallest available choice is therefore 0. Example 3 n = 3 parentChild = [] return = [0,1,2] All resources are immediately deletable, so the smallest-ID rule returns them in ascending order. 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is Kahn's algorithm with a min-heap. For each pair [parent, child], the parent waits on the child. So track a count per parent of how many children remain undeleted. Every resource with a count of 0 goes into a min-heap. Pop the smallest, append it to the answer, then for each of its parents decrement the count. When a parent hits 0, push it. The common pitfall is using a plain queue, which gives a valid order but not the smallest-first one. Example 2 shows it: after deleting 2, only 0 unlocks, so you pick 0 before 3. Build a reverse adjacency list from child to parents. Complexity is O((n + m) log n), fine for n up to 100000 and 200000 edges. Use iterative code, not recursion. StealthCoder is the hedge if the heap detail slips under pressure.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Parent-Child Deletion Order cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Parent-Child Deletion Order FAQ
What's the trick in this Google OA problem?+
It's a topological sort with a min-heap instead of a queue. Each parent waits for all its children to be deleted. Push every resource with zero remaining children into the heap, pop the smallest, and release its parents as their counts hit zero.
Which way do the edges go?+
Each row is [parent, child], and the child must be deleted first. So the child unlocks the parent. Store adjacency from child to parents, and keep a count of remaining children per parent. Reversing this is the most common mistake.
Why not just use a regular queue?+
A queue returns a valid topological order but ignores the smallest-ID rule. Example 2 expects [2,0,3,1]. A FIFO queue could emit 3 before 0 depending on insertion order. The heap guarantees the smallest available resource every time.
How hard is this really?+
Medium. If you know Kahn's algorithm, it's a small change. The constraints of 100000 nodes and 200000 edges rule out anything slower than O((n + m) log n). Watch for disconnected resources, which start with zero children and must still appear.
How do I prepare in 48 hours?+
Write Kahn's algorithm from memory twice, once with a queue and once with a heap. Test it on the three examples, including the empty edge list. Practice building the reversed adjacency list quickly, since that's where first attempts go wrong.