Single-Nested Valid Bracket Substrings
Reported by candidates from Wells Fargo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Wells Fargo OA reported in September 2020 hands you a valid bracket string and asks for every matched pair as its own substring. It looks like a warm-up. The trap is in the ordering and the duplicates, and that's where a quick solution quietly fails. This is a stack problem with one twist: output order follows opening positions, not closing ones. If you've got an invite in your inbox, know this shape before you sit down. StealthCoder is the safety net if your mind goes blank mid-assessment, but the idea fits in a few lines.
The problem
Given a valid bracket sequence s built from round, square, and curly brackets, return every substring that forms one single nested segment.
Each opening bracket and its matching closing bracket define exactly one such segment. A segment may contain any number of complete child segments, but a concatenation of sibling segments is not itself one segment unless another matching pair encloses it.
Return the segments in increasing order of their opening positions. If equal substring values occur at different positions, retain every occurrence.
Function
singleNestedSegments(s: String) → String[]
Examples
Example 1
s = "()[()]{[][]}"
return = ["()","[()]","()","{[][]}","[]","[]"]
The opening positions occur in the order 0, 2, 3, 6, 7, 9. The two equal [] segments come from different matched pairs, so both remain in the output.
Example 2
s = "{}{[]}"
return = ["{}","{[]}","[]"]
The whole string is a concatenation of two top-level segments, so it is not returned as one segment. Its three matched pairs contribute {}, {[]}, and [].
Example 3
s = "{[()]}"
return = ["{[()]}","[()]","()"]
The three opening brackets appear from outermost to innermost, so their matched substrings are returned in that order.
Constraints
1 <= s.length <= 2000.
s contains only (, ), [, ], {, and }.
s is a valid bracket sequence and may contain multiple top-level segments.Reported by candidates. Source: FastPrep
Pattern and pitfall
Push the index of every opening bracket onto a stack. When you hit a closing bracket, pop the top index. That index and the current position define one segment, s[open..close]. The edge case that breaks a naive solution is order. Pairs finish closing innermost first, so if you append on close, your output comes out in closing order and fails Example 1. Fix it by preallocating a result slot per opening index, or by storing (open, substring) and sorting by open. Another clean option is a result array of size n, filled at index open, then compacted. Don't dedupe. Equal substrings at different positions must all stay, so skip any set. Also don't merge siblings like {}{[]}. Each pair is its own segment. With n up to 2000, substring slicing is fine. StealthCoder is your hedge in the live OA if the ordering detail slips your mind.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Single-Nested Valid Bracket 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Wells Fargo's OA.
Wells Fargo reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Single-Nested Valid Bracket Substrings FAQ
What's the trick in this Wells Fargo bracket problem?+
Use a stack of opening indices. On each closing bracket, pop the index and record the substring from that index to the current position. The only real trick is output order. You must return segments sorted by opening position, not by when they close.
Why does my output order come out wrong?+
Matched pairs close innermost first, so appending on close gives you inner segments before outer ones. Example 3 expects the outermost first. Store each substring in a slot indexed by its opening position, then read the slots in order, or sort by the opening index.
Do I need to dedupe equal substrings?+
No. Example 1 keeps both [] segments because they come from different matched pairs. Use a list, never a set. Every matched pair produces exactly one output entry, so the result length equals half the string length.
Should the whole string count when it's a concatenation?+
Only if one matching pair encloses it. In Example 2, {}{[]} is two top-level segments, so the full string isn't returned. The stack approach handles this on its own, since it only emits real matched pairs.
How do I prepare for this in 48 hours?+
Write the stack solution from scratch twice. Test it on the three examples, plus a single pair and a deeply nested string. Check that your order matches by opening index. Do you need to validate brackets? No, the input is guaranteed valid, so skip mismatch handling.