Sort Every N-ary Tree Node's Children
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in November 2020, and it looks scarier than it is. The title says tree, the input says graph, but strip the wrapper and it's a sort with a custom comparator. Every row of children gets sorted by the child's value, then by the child's index. No traversal needed. If you've got an OA invite for this, the job is spotting that quickly and not overbuilding. StealthCoder sits invisibly on your screen as a safety net if you blank on the comparator syntax mid-assessment, but the idea is small enough to hold in your head.
The problem
An N-ary tree is encoded by parallel arrays. Node i has value values[i], and children[i] contains the indices of its children. Return a copy of children where every row is sorted by ascending child-node value, breaking equal-value ties by ascending child index. Function sortNaryChildren(values: int[], children: int[][]) → int[][] Examples Example 1 values = [10,5,7,5] children = [[2,1,3],[],[],[]] return = [[1,3,2],[],[],[]] Children with value 5 come before value 7; indices 1 then 3 break the tie. Constraints 1 <= values.length == children.length <= 10^5. Child relationships form one rooted tree with root index 0.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's what it reduces to: for each row in children, sort the indices using the key (values[child], child). That's it. You never walk the tree, so no DFS, no BFS, no recursion. Total work is the sum of row lengths times log, and since the tree has at most 10^5 nodes, the total number of child entries is under 10^5. That's comfortable. The pitfall is mutating the input when the problem asks for a copy, so build new rows. Another is sorting by value only and relying on stability, which breaks if a row isn't already in index order. Use the tuple key explicitly. Also watch recursion habits: a deep tree could blow the stack, and you don't need recursion at all. If the comparator syntax slips in your language under pressure, StealthCoder is the hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Sort Every N-ary Tree Node's Children 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort Every N-ary Tree Node's Children FAQ
What's the trick in Sort Every N-ary Tree Node's Children?+
There isn't a tree trick. Each row is just a list of indices to sort with key (values[idx], idx). The tree framing is a distraction. Write one loop over rows, sort each with that key, and return the new arrays.
How hard is this Bloomberg OA question really?+
Easy. It's a custom sort applied per row. The only real risks are forgetting the index tiebreak, mutating the input instead of copying, or overcomplicating it with traversal. Ten minutes if you stay calm.
Do I need DFS or BFS here?+
No. Rows are independent, so order of processing doesn't matter. The root and tree shape guarantees don't change anything. Iterate through children once and sort each row. Skipping traversal also avoids stack overflow on deep trees.
What's the time complexity?+
Sorting each row costs k log k for k children. Since every node except the root is a child exactly once, total entries are n-1, so the whole thing is O(n log n) worst case. Space is O(n) for the copied output.
How do I prepare in 48 hours for this kind of OA?+
Practice custom comparators and tuple keys in your language until they're automatic. Then do a few array and sorting problems with tiebreaks. This Bloomberg problem from November 2020 rewards reading carefully more than knowing exotic algorithms.