FastPrepBalanced Split String with Wildcards
Problems/Balanced Split String with Wildcards

Balanced Split String with Wildcards

Two Sigma logoTwo Sigma● MediumOA

Updated Aug 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The screenshot is a little blurry around the sample line and cuts off immediately after the constraints. FastPrep kept only the visible limits and worked the sample out strictly from the visible characters and stated rules. If we come across a fuller version later, we will update this.

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:

  1. 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 ()[].
  2. 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 ?.

More Two Sigma Online Assessment questions

  • IPO Share AllocationOA · Seen Sep 2026
  • Sewer Drainage PartitionOA · Seen Sep 2026
  • Daily Temperature By TownOA · Seen Sep 2026
  • Linear InterpolatorOA · Seen Sep 2026
  • Online No-Intercept Linear RegressionOA · Seen Sep 2026
  • Closest ColorOA · Seen Aug 2026
  • Piecewise Linear Interpolation and ExtrapolationOA · Seen May 2026
  • Calculate y/x using PatchOA · Seen Nov 2025
  • Nums That Are Divisible by N 🍋OA · Seen Mar 2024
  • Replacing Val 🍓OA · Seen Mar 2024

Similar questions at other companies

  • Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
  • Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
  • Earliest Time to Schedule a MeetingMicrosoft · OAMicrosoft interview questions
  • Maximum Equalized Substrings After One Adjacent SwapVisa · OAVisa interview questions
  • Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
  • Longest Balanced Substring After One SwapTekion · OATekion interview questions
  • Shortest Compressed Length After RemovalDRW · OADRW interview questions
  • Count Special StringsAmazon · OA
  • Binary Palindromic Subsequences of Length 5Citadel · OACitadel interview questions
  • Maximum Positive FeedbackTikTok · OATikTok interview questions
All Two Sigma interview questionsSee Two Sigma hiring insights
Saved
class Solution {
    public int countBalancedSplits(String s) {
      // write your code here
    }

}
Checking account…