Reported January 2022
Bloombergdepth first search

Lexicographically Smallest Root-to-Leaf String

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

One null child can quietly wreck your answer on this Bloomberg OA, reported in January 2022. Each node holds 0 to 25, you map it to a-z, and you return the smallest root-to-leaf string. It's a tree DFS, and the trap is the node with only one child. Treat the missing side as a leaf and you return a shorter string that looks falsely smaller. An empty tree returns an empty string. With up to 10^5 nodes, a skewed tree can also blow your recursion stack. Know both edges before you type. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time as a safety net.

The problem

Each binary-tree node contains an integer from 0 through 25 representing letters a through z. Return the lexicographically smallest string encountered along a path from the root to any leaf.
Return the empty string for an empty tree.

Function
smallestRootToLeaf(root: TreeNode) → String

Examples
Example 1
root = [0,1,2,3,4,3,4]
return = "abd"
The root-to-leaf strings are abd, abe, acd, and ace; abd is smallest.

Constraints
The tree contains at most 10^5 nodes.
Every node value is between 0 and 25.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is DFS with a path buffer. Push the node's letter, recurse into whichever children exist, and only compare against the best answer when both children are null. That leaf check is the whole problem. If you test for a null node instead of a leaf, a parent with one child ends the path early and a prefix like "ab" beats "abd" even though it isn't a valid root-to-leaf string. Don't try to greedily pick the smaller child either. Equal letters on both sides mean you must explore both subtrees. Build strings from a list of characters and pop on the way back, not repeated concatenation. For depth up to 10^5, use an iterative stack or watch your language's recursion limit. If the live OA freezes you on any of this, StealthCoder is the hedge sitting invisibly on your screen.

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 Lexicographically Smallest Root-to-Leaf String 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.

Lexicographically Smallest Root-to-Leaf String FAQ

What's the trick in the Bloomberg smallest root-to-leaf string problem?+

Only compare strings at true leaves, meaning both children are null. Walk the tree with DFS, keep the current path, and update the best answer at each leaf. Don't stop at a node just because one child is missing, or you'll return a prefix that isn't a real root-to-leaf path.

Can I just pick the smaller child at each step?+

No. If both children hold the same letter, or a smaller child leads to a larger suffix later, greedy picks the wrong branch. You have to explore every root-to-leaf path and compare the finished strings. DFS handles that cleanly in one pass over the tree.

How do I handle a tree with 10^5 nodes?+

Watch recursion depth. A fully skewed tree is 10^5 levels deep, which can overflow the default stack in many languages. Use an iterative DFS with an explicit stack, or raise the recursion limit if your language allows it. Also avoid rebuilding strings at every node.

What should I return for an empty tree?+

The empty string, per the problem statement. Check for a null root first and return immediately. It's an easy point to lose if your code assumes at least one node and tries to read root.val before checking.

How do I prepare for this in 48 hours?+

Write the DFS solution twice from scratch. Once recursive, once iterative. Then test three cases: single node, a chain where each parent has one child, and a tree with equal letters on both branches. Those cover the real failure points for this problem.

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