Reported February 2025
Bloombergdepth first search

Minimum-Cost Root-to-Leaf Path in an N-ary Tree

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

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

Bloomberg reported this one in February 2025, and it looks scarier than it is. Strip the wording and it's a tree walk: find the cheapest root-to-leaf sum, then return the cost followed by the actual path with ties broken lexicographically by index. If your OA invite lands in the next day or two, this is the pattern to lock in. Depth is the real danger, since n can hit 10^5. StealthCoder is the safety net running invisibly during the live OA if you blank on the tie-break logic, but the core idea fits in your head.

The problem

Node i has value values[i] and child indices children[i]; root is 0. Path cost is the sum of node values from root through a leaf.
Return a long array containing cost first, followed by the node indices of a minimum-cost root-to-leaf path. Break cost ties lexicographically by index sequence.

Function
minimumCostPath(values: int[], children: int[][]) → long[]

Examples
Example 1
values = [5,2,3,1,4]
children = [[1,2],[3,4],[],[],[]]
return = [8,0,1,3]
Path 0,1,3 costs 5+2+1=8, less than the other leaves.

Constraints
1 <= values.length == children.length <= 10^5.
Children form a rooted tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

What it really reduces to: a DFS over a rooted tree, computing the best cost from each node down to a leaf. At each node, best(node) = values[node] + min over children of best(child), or just values[node] if it's a leaf. Then rebuild the path by walking down from the root and picking the child with the smallest best value. For ties, pick the smaller index sequence. Comparing whole paths at every node is the pitfall, because it blows up the runtime. Pick the smaller child index among equal costs, but check that the sequences really compare that way since children aren't guaranteed sorted. Use long for sums. Recursion at depth 10^5 will overflow the stack in many languages, so go iterative with a post-order stack. If the tie-break or the iterative conversion trips you up live, StealthCoder is the hedge that hands you a working version.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Minimum-Cost Root-to-Leaf Path in an N-ary Tree 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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum-Cost Root-to-Leaf Path in an N-ary Tree FAQ

What's the trick in this Bloomberg minimum-cost path problem?+

Compute the best cost to a leaf for every node bottom-up, then walk from the root choosing the child with the lowest best cost. That's one pass to compute and one pass to reconstruct. No need to store every path, which would be too slow for 10^5 nodes.

How do I handle the lexicographic tie-break?+

When two children have equal best cost, the path sequence starts the same up to the current node, so the next index decides. Choose the smaller child index. Don't assume children are sorted, so compare explicitly. Since subtrees are disjoint, indices differ immediately and the first difference settles it.

Will recursion work with 10^5 nodes?+

Risky. A skewed tree gives depth 10^5, and that overflows the default stack in many languages. Use an iterative post-order traversal with an explicit stack, or process nodes in reverse BFS order. Either way you get children before parents without recursion.

Why does the return type use long?+

Node values are ints and a path can have up to 10^5 nodes, so the sum can exceed the 32-bit range. Accumulate in long and put the cost first in the returned long array, followed by the node indices from root to leaf.

How do I prepare for this in 48 hours?+

Write tree DP once from scratch: bottom-up min cost plus top-down reconstruction. Then redo it iteratively. Test on a single node, a long chain, and a tree with tied leaves. That covers the edge cases this question is built around.

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

OA at Bloomberg?
Invisible during screen share
Get it