MEDIUMasked at 5 companies

Minimum Number of Swaps to Make the String Balanced

A medium-tier problem at 78% community acceptance, tagged with Two Pointers, String, Stack. Reported in interviews at Expedia and 4 others.

Founder's read

You've got a string of brackets that's not balanced. The problem asks how many swaps you need to fix it. Expedia, Twilio, and PayPal have all asked this one. It's medium difficulty with a 78% acceptance rate, so most people who see it solve it. The trick isn't obvious on first read though. You might think you need a stack or multiple passes. You don't. The real solution is greedy and uses two pointers to count mismatches in a single pass. If this problem hits your live OA and you blank on the pattern, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
5
Difficulty
MEDIUM
Acceptance
78%

Companies that ask "Minimum Number of Swaps to Make the String Balanced"

If this hits your live OA

Minimum Number of Swaps to Make the String Balanced is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The problem wants the minimum number of swaps to balance brackets. The naive instinct is to simulate swaps or use a stack, but both are overkill. The greedy move is to iterate left to right, tracking open brackets. When you hit a close bracket without a matching open, you've found a mismatch. Count those mismatches, then the answer is mismatches divided by 2 (integer division), because each swap fixes two mismatches at once. Two pointers or a counter work equally well here. The acceptance rate of 78% suggests most candidates either nail the greedy insight or spend time on a correct but inefficient approach. The topics are Two Pointers, String, Stack, and Greedy. You only need Two Pointers and Greedy in practice. If you start building a stack or trying to simulate actual swaps, you're overcomplicating it. StealthCoder is your hedge if the greedy pattern doesn't click during the assessment.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Number of Swaps to Make the String Balanced recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Number of Swaps to Make the String Balanced interview FAQ

Why is the answer just mismatches divided by 2?+

Each swap moves one open bracket right and one close bracket left. So one swap fixes exactly two unmatched bracket pairs. Count the total mismatches in one pass, divide by 2. The greedy approach works because fixing the leftmost mismatches first always yields the minimum.

Is a stack solution wrong?+

No, a stack solution is correct but slower. You'd push open brackets, pop on close brackets, count unmatched closes. It works but requires extra memory and multiple passes. Two pointers or a simple counter is O(1) space and single pass.

Do companies still ask this one frequently?+

Yes. Expedia, Twilio, PayPal, and others have reported it recently. It's medium difficulty, so it slots into phone screen or early OA rounds. The 78% acceptance rate means it's not a filter problem. It tests greedy thinking more than raw coding.

What's the trap if I iterate from left to right?+

The trap is overthinking it. You don't need to track the specific positions of brackets or simulate swaps. Just count: for each character, track open brackets. If you see a close without an open, increment a mismatch counter. That's it.

How does this relate to the Stack and Two Pointers topics?+

Stack is a red herring for most solutions. Two Pointers or a counter-based approach is optimal. The problem tests your ability to reject the obvious toolbox answer and see the greedy pattern instead.

Want the actual problem statement? View "Minimum Number of Swaps to Make the String Balanced" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.