Reported September 2026
OpenAItree

Aggregate Machine Topology

Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live OpenAI OA. Under 2s to a working solution.
Founder's read

OpenAI reported this one in September 2026, and the constraint that matters is n up to 100000 with machine IDs as large as 10^9. That kills any approach that scans the parent array for children at each node, and it kills recursion that assumes a shallow tree. The task is a rooted tree build, a sorted-children preorder walk, and a subtree size for every node. If you blank on the iterative version during the OA, StealthCoder is the invisible safety net running on your screen. But the pattern is simple once you see it.

The problem

A cluster snapshot contains one report from every machine. For each index i, machineIds[i] is a unique machine ID and parentIds[i] is the parent machine's ID. Exactly one machine has parent -1; it is the root. The remaining parent relationships form one rooted tree.
Return a canonical topology summary. Each output row is [machineId, parentId, depth, subtreeSize], where depth counts edges from the root and subtreeSize counts the machine itself and every descendant.
Rows must use preorder traversal. When a machine has multiple children, visit them in increasing machine-ID order. The root row therefore exposes the total machine count in its subtreeSize field.

Function
aggregateMachineTopology(machineIds: int[], parentIds: int[]) → int[][]

Examples
Example 1
machineIds = [30,10,20]
parentIds = [10,-1,10]
return = [[10,-1,0,3],[20,10,1,1],[30,10,1,1]]
Machine 10 is the root. Its children are visited as 20 and then 30. The root subtree contains all three machines.
Example 2
machineIds = [7,2,9,1,5,3]
parentIds = [3,1,3,-1,2,1]
return = [[1,-1,0,6],[2,1,1,2],[5,2,2,1],[3,1,1,3],[7,3,2,1],[9,3,2,1]]
The root has children 2 and 3. Machine 2 owns a two-machine subtree, while machine 3 owns a three-machine subtree. Preorder visits each parent's increasing-ID children before returning to later siblings.

Constraints
1 <= machineIds.length == parentIds.length <= 100000.
Every machine ID is unique and lies in [0, 10^9].
Exactly one value in parentIds is -1.
Every other parent ID occurs in machineIds, and all parent relationships form one rooted tree.
The output rows use deterministic preorder with children sorted by increasing machine ID.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a hash map from parent ID to a list of child IDs in one pass. Sort each child list ascending. Find the root, the one machine with parent -1. Then run an iterative DFS with an explicit stack, because a chain of 100000 machines will blow the recursion limit in most languages. Record depth on the way down and preorder position as you emit rows. Subtree size needs a post-order step. Either compute sizes in a second pass over the reversed preorder, adding each node's size to its parent, or keep enter and exit markers on the stack. The pitfall is indexing by raw ID in an array. IDs go to 10^9, so use a map or compress them. Total cost is O(n log n) from sorting. If you freeze on the iterative size accumulation, StealthCoder can hand you the working version live.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Aggregate Machine Topology 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass OpenAI's OA.

OpenAI reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Aggregate Machine Topology FAQ

How hard is Aggregate Machine Topology really?+

Medium. Nothing exotic, but you combine three things: build a tree from a parent array, sorted preorder, and subtree sizes. Each is easy alone. The risk is stitching them together cleanly without recursion depth or ID-indexing bugs under time pressure.

What's the trick to getting subtree sizes?+

Do a preorder traversal and store the order. Then walk that order in reverse, adding each node's size into its parent's size. Children always appear after parents in preorder, so reversed order guarantees children finish first. No second recursion needed.

Why can't I just use recursion?+

With 100000 machines, the tree can be a single chain 100000 deep. That overflows the call stack in Python and often in Java. Use an explicit stack. Push children in descending ID order so the smallest ID pops first and preorder stays correct.

Do I need to sort the whole input by ID?+

No. You only need each parent's child list sorted. Group children by parent in a hash map, then sort each list. Total sorting cost across all lists is at most O(n log n), which is fine for n of 100000.

How do I prepare for this in 48 hours?+

Practice building an adjacency map from a parent array and writing an iterative preorder DFS from memory. Add the reverse-order size accumulation. Test on a chain, a star, and the two given examples. That covers nearly every failure mode here.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with OpenAI.

OA at OpenAI?
Invisible during screen share
Get it