Minimum Removal for Balanced Parentheses
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem in this Meta OA, reported in August 2026, comes down to one stack, and if you see it fast the rest is typing. You get a string of letters, digits and parentheses, and you strip the fewest parentheses to make it balanced. Letters never go. Order never changes. It's a classic parentheses cleanup, and candidates blank on it because the tie-break rule looks scarier than it is. If you freeze mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the approach while the proctor sees nothing.
The problem
Given a string s containing ASCII letters, digits, opening parentheses, and closing parentheses, remove the minimum possible number of parentheses so that the remaining parentheses are balanced. You may not add or reorder characters.
Use this deterministic rule when several minimum-removal results are possible:
Scan from left to right. Discard every closing parenthesis that has no unmatched opening parenthesis before it.
After that scan, discard the still-unmatched opening parentheses from right to left.
Return the retained characters in their original relative order. Letters and digits are always retained.
Function
makeParenthesesBalanced(s: String) → String
Examples
Example 1
s = "lee(t(c)o)de)"
return = "lee(t(c)o)de"
The final closing parenthesis has no matching opening parenthesis, so removing it produces a balanced result with one deletion.
Example 2
s = "a)b(c)d"
return = "ab(c)d"
The closing parenthesis after a is unmatched during the left-to-right scan. Every other parenthesis can be retained.
Example 3
s = "))(("
return = ""
Both closing parentheses are unmatched, and both opening parentheses remain unmatched, so all four are removed.
Constraints
1 <= s.length <= 100000.
s contains only ASCII letters, digits, (, and ).Reported by candidates. Source: FastPrep
Pattern and pitfall
Use a stack of indices. Walk the string left to right. On '(' push its index. On ')' pop if the stack is non-empty, otherwise mark that index for removal. After the scan, every index still on the stack is an unmatched opening paren, so mark those too. That's exactly the rule in the statement: closers with no opener before them go first, then leftover openers. Build the result by skipping marked indices. It's O(n) time and O(n) space, which matters with length up to 100000. The common pitfall is pushing characters instead of indices, which leaves you unable to delete the right position. Another is a counter-only approach that handles closers but forgets which openers to drop. Use a boolean array or a set for removals, then join. If the stack idea slips away under pressure, StealthCoder is the hedge for 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 Minimum Removal for Balanced Parentheses 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
This OA pattern shows up on LeetCode as minimum remove to make valid parentheses. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Meta's OA.
Meta 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.
Minimum Removal for Balanced Parentheses FAQ
What's the trick in the Meta balanced parentheses removal problem?+
Store indices of unmatched opening parentheses on a stack. When a closing paren shows up with an empty stack, mark it for removal. After the scan, whatever indices remain on the stack are unmatched openers, so remove those too. Then rebuild the string skipping marked indices.
How hard is this problem really?+
It's medium-easy once you know the stack-of-indices idea. The logic is short, maybe fifteen lines. The difficulty is recognizing that you need positions, not characters, and handling the leftover openers after the scan. Most failures are off-by-one or forgetting the second cleanup step.
Can I solve it without a stack?+
Yes. Do two passes with a counter. Left to right, drop closers when the open count is zero. Then right to left, drop extra openers. It's O(n) with less memory. But the stack version maps directly to the stated rule and is easier to get right under time pressure.
What edge cases should I test?+
Test a string with no parentheses, which returns unchanged. Test '))((' which returns empty. Test all openers like '(((' and all closers. Also test letters mixed between unmatched parens, like 'a)b(c', to confirm the order of kept characters stays intact.
How do I prepare for this in 48 hours?+
Write the stack solution from memory twice, once with indices and once with the two-pass counter. Run the three given examples by hand. Then try a 100000-character input mentally to confirm linear time. Skip broad review. This pattern is narrow and a couple of clean reps is enough.