Deepest Nested Substrings
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in December 2025 looks like a bracket-parsing puzzle, but it boils down to one pass with a depth counter. You track how deep you are, find the max depth, and collect the contents of every pair that hits it. Bloomberg reportedly asked for results left to right, with empty pairs returning an empty string. If you've got the OA in a day or two, this one is very doable. StealthCoder sits invisibly as a backup in case you blank on the stack details mid-assessment, but the logic below should be enough.
The problem
Given a balanced string containing lowercase letters and the bracket pairs (), [], and {}, return the contents of every bracket pair at the maximum nesting depth.
Return results from left to right. The returned content excludes the surrounding brackets. An empty deepest pair contributes the empty string.
Function
deepestNestedSubstrings(expression: String) → String[]
Examples
Example 1
expression = "a[bc]def{cd}"
return = ["bc","cd"]
Both pairs are at depth one, the maximum depth.
Example 2
expression = "ran(n(d))o(m())"
return = ["d",""]
The pairs around d and the empty string are the depth-two pairs.
Example 3
expression = "x{a[b(c)d]e}y"
return = ["c"]
The innermost parentheses are at depth three.
Constraints
0 <= expression.length <= 10^5.
The expression contains lowercase English letters and balanced, correctly matched brackets.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you don't need to match bracket types, because the input is guaranteed balanced and correctly matched. Use a stack of start indices. On any opener, push the index after it and record the new stack size as the current depth. On any closer, pop the start index and slice from start to the closer's index. Keep a best depth and a results list. When a pair closes at a depth greater than best, clear the list and set best. If equal, append. Depth is the stack size before popping. The common pitfall is forgetting that a pair containing nested pairs isn't deepest, and forgetting the empty string case like m(). Another is rebuilding substrings char by char, which can go quadratic on 10^5 characters. Slice once per pair. Total work is linear. If your brain freezes in the live OA, StealthCoder can hand you this stack skeleton as a hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Deepest Nested Substrings 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 StealthCoderRelated leaked OAs
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.
Deepest Nested Substrings FAQ
What's the trick in Deepest Nested Substrings?+
Use a stack of opening indices. When a closer arrives, the stack size before popping is that pair's depth. Track the max depth seen, reset your result list when you find a deeper pair, and append when it ties. One pass, linear time.
Do I need to check that bracket types match?+
No. The problem states the expression is balanced and correctly matched, so () [] and {} can be treated identically. Any opener pushes, any closer pops. Skipping type checks keeps the code short and removes a whole class of bugs.
How do I handle empty deepest pairs like in ran(n(d))o(m())?+
Slicing from start index to closer index gives an empty string naturally when they're adjacent. In that example the pair () around nothing is depth two, same as the one around d, so you return ["d",""]. Don't filter out empties.
Why might my solution time out on 10^5 characters?+
Usually because of repeated string concatenation or re-scanning the substring for every pair. Store start indices and slice once on close. Also avoid clearing and rebuilding lists excessively. A single pass with one slice per qualifying pair stays linear.
How do I prepare for this in 48 hours?+
Practice two or three bracket-stack problems until pushing indices feels automatic, then write this one from scratch using the three examples as tests. Focus on edge cases: empty string input, no brackets at all, and a single empty pair. That covers most of what Bloomberg would check.