Remove Invalid Parentheses
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A Bloomberg OA reported in October 2020 asked for Remove Invalid Parentheses, and the whole thing hinges on a set. You're generating candidate strings, and without a hash set to dedupe them you'll output the same answer five times and fail. You also need lexicographic order at the end. The input is tiny, 25 characters max, which tells you brute-force BFS or backtracking is the intended road. If you blank on the live OA, StealthCoder sits invisibly on your screen as a safety net. Know the shape first though: minimum removals, distinct results, sorted output.
The problem
Remove the minimum number of parentheses from text so every remaining parenthesis string is valid. Letters and other non-parenthesis characters cannot be removed. Return every distinct minimum-removal result in lexicographic order. Function removeInvalidParentheses(text: String) → String[] Examples Example 1 text = "()())()" return = ["(())()","()()()"] Removing one closing parenthesis yields the two distinct valid results. Constraints 0 <= text.length <= 25.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is level-by-level BFS with a visited set. Start with the original string. If any string in the current level is valid, collect all valid ones from that level and stop. Otherwise, generate every string with one more parenthesis removed, add unseen ones to the set, and go to the next level. Because you stop at the first level with a valid string, the removal count is minimal automatically. The common pitfall is removing letters, or skipping the dedupe and blowing up on repeated characters like "((". Another is forgetting the empty string case, where the answer is [""]. Validity check is a simple counter that never goes negative and ends at zero. Sort the results before returning. Backtracking with precomputed extra left and right counts also works and prunes harder. If the BFS logic slips under pressure, StealthCoder can hand you a working version during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Remove Invalid 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as remove invalid parentheses. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Remove Invalid Parentheses FAQ
What's the trick to Remove Invalid Parentheses?+
Use BFS over removal count. Level zero is the original string, level one removes one character, and so on. Stop at the first level containing a valid string. A visited set prevents duplicates, and the first valid level guarantees the minimum number of removals.
How hard is this really for an OA?+
It's a hard-tagged problem, but the 25 character limit makes brute force acceptable. The difficulty is dedupe and stopping at the right level. If you know BFS with a set, you can write it in 15 minutes.
Do I need to sort the output?+
Yes. This version asks for lexicographic order, so sort the final list before returning. Collecting results in a set and calling sort once at the end is the cleanest way and avoids ordering bugs from BFS traversal.
What edge cases break most solutions?+
Empty string should return a list with one empty string. Strings with no parentheses return themselves. Strings that are already valid return unchanged. Letters must never be removed, so only generate candidates by deleting '(' or ')'.
How do I prepare in 48 hours?+
Write the BFS version from scratch twice, then the backtracking version with left and right removal counts. Test on "()())()", "(a)())()", and ")(". Practice the validity counter until it's automatic. That covers the Bloomberg variant reported in October 2020.