Balanced Split String with Wildcards
Reported by candidates from Two Sigma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Two Sigma problem is treating it like the classic bracket-matching stack question. It's reported from July 2026, and the word "rearranged" changes everything. Order doesn't matter, only counts. You split a string of (, ), [, ], and ? into two non-empty pieces, and each piece must be turnable into a balanced string after you swap the ? characters. With length up to 10^5, brute-forcing every split with a fresh scan won't survive. It's a string problem with prefix counts underneath. If you blank during the live OA, StealthCoder runs invisibly as a safety net.
The problem
Determine the number of ways a string containing the characters (, ), [, ], and ? can be divided into two non-empty substrings such that each substring can be rearranged to form a balanced string.
The ? characters can be replaced with any bracket character, (, ), [, or ], as needed to achieve balance.
The two substrings together must cover the entire original string and cannot overlap. A substring is a contiguous block of the original string.
Balanced strings
A balanced string has all brackets properly matched and nested. For example, [], (), and [()] are balanced. Strings such as (], ([), and ] are not.
Function
countBalancedSplits(s: String) → int
Examples
Example 1
s = "?()?[?"
return = 2
The string has two valid splits:
s1 = "?(" and s2 = ")?[?". Replace the ? in s1 with ) so s1 can be rearranged into (). Replace the ? characters in s2 with ( and ], so s2 can be rearranged into ()[].
s1 = "?()?" and s2 = "[?". Replace the ? characters in s1 with [ and ], so s1 can be rearranged into ()[]. Replace the ? in s2 with ] to make [].
Therefore, the total number of valid splits is 2.
Constraints
4 <= length of s <= 10^5
s contains only (, ), [, ], and ?.Reported by candidates. Source: FastPrep
Pattern and pitfall
Since each substring can be rearranged, nesting order is irrelevant. A substring is feasible when the paren count and square count can each be paired up. Let a be the count of (, b the count of ), c the count of [, d the count of ], and q the count of ?. You need |a-b| + |c-d| <= q, and the leftover ? after fixing imbalances must be even, which means (a+b+c+d+q) is even and the length is even. The pitfall is running a stack and rejecting valid rearrangeable strings. Another trap is recomputing counts per split, which makes it O(n^2). Build prefix counts for all five characters, then for each split point check both sides in O(1). Total is O(n). If the parity or surplus-? logic slips under pressure, StealthCoder is the hedge in the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Balanced Split String with Wildcards 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Two Sigma's OA.
Two Sigma reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Balanced Split String with Wildcards FAQ
What's the trick in Balanced Split String with Wildcards?+
Rearrangement means order is irrelevant, so you only track counts. For each side, compute the paren imbalance and the square imbalance, then check that the ? characters can cover both and that the leftover is even. No stack needed.
How hard is this one really?+
Medium. The code is short, but the reasoning about wildcards and parity is where people slip. Once you see it's count-based with prefix sums, the implementation takes a few minutes. The first-attempt trap is reaching for a stack.
What time complexity does Two Sigma expect here?+
With n up to 10^5, you need O(n). Precompute prefix counts of each of the five characters, then test each split point in constant time. Anything that rescans each substring per split is O(n^2) and will likely time out.
What edge cases should I test?+
Test odd-length substrings, which can never balance. Test all-? strings, where any even-length piece works. Test a side with only ( characters and no ?, which fails. Also confirm both substrings are non-empty, so splits run from index 1 to n-1.
How do I prepare for this in 48 hours?+
Write the prefix-count solution once from scratch and run the example, which should return 2. Then practice the balance check: imbalance of parens plus imbalance of squares must be at most the ? count, with matching parity. That's the whole problem.