Reported September 2026
Mygatebreadth first search

Remove Invalid Parentheses

Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Mygate OA. Under 2s to a working solution.
Founder's read

Mygate reported this one in September 2026, and the whole solution hinges on a set. Remove Invalid Parentheses looks like a brute-force nightmare, but the constraints (length 20, at most 16 parentheses) are small on purpose. If your OA lands in the next couple of days, this is the shape to expect: strip the fewest brackets, keep every distinct valid result, return them sorted. Dedupe is where people lose points. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you can walk in already knowing the trick.

The problem

Given a string s containing parentheses and English letters, remove the fewest parentheses necessary to make the remaining parentheses valid. Letters cannot be removed and retain their original order.
A valid string has no prefix containing more closing parentheses than opening parentheses, and its total numbers of opening and closing parentheses are equal.
Return every distinct string attainable using the minimum number of removals, in lexicographic order. Duplicate results appear once. If the original string is valid, return only that string. The empty string is valid.

Function
removeInvalidParentheses(s: String) → List<String>

Examples
Example 1
s = "()())()"
return = ["(())()","()()()"]
Deleting one of the extra closing parentheses yields two distinct valid strings. No zero-removal result is valid.
Example 2
s = "(a)())()"
return = ["(a())()","(a)()()"]
The letter a is preserved in each of the two minimum-removal results.

Constraints
0 ≤ s.length ≤ 20.
s contains only English letters and the characters ( and ).
s contains at most 16 parentheses.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The core is a hash set of strings. Two clean approaches work. First, BFS by removal count: start with s in a set, check if any member is valid, and if not, generate every string with one character removed (only parentheses), dedupe through the set, and repeat. Stop at the first level that has a valid string. That guarantees minimum removals. Second, DFS with precomputed counts: scan once to find how many extra opening and closing parentheses must go, then backtrack, skipping duplicates. The pitfall is returning results from a deeper level, which breaks the minimum rule. Another is forgetting the final sort, since the output must be lexicographic. Also remember letters are never removed, and an empty string counts as valid. If you freeze on the live OA, StealthCoder can hand you the working structure fast, but the BFS-with-set version is short enough to memorize.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as remove invalid parentheses. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Mygate's OA.

Mygate reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Remove Invalid Parentheses FAQ

What's the trick to Remove Invalid Parentheses?+

Use BFS by number of removals with a visited set. Each level removes one more parenthesis from every string in the previous level. The first level containing any valid string is the minimum, so you collect all valid ones there and stop. The set handles duplicates automatically.

How hard is this really given the constraints?+

Easier than it looks. With at most 16 parentheses, exponential search is fine. The hard part isn't speed, it's correctness: deduping results, stopping at the minimum level, and sorting the output. Get those three right and it passes.

How do I check if a string is valid?+

Keep a counter. Add one for an opening parenthesis, subtract one for a closing one, ignore letters. If the counter ever goes negative, it's invalid. At the end it must be zero. That one pass covers both the prefix rule and the equal-total rule.

Do I need to sort the output?+

Yes. The problem asks for results in lexicographic order. Collect valid strings into a list and sort it before returning. Easy to forget when your BFS already produces a neat-looking set, since set order is not guaranteed.

How do I prepare for this in 48 hours?+

Write the BFS version from scratch twice, then the backtracking version once. Test on the two examples plus edge cases: an empty string, a string with no parentheses, and an already valid string. Check that the valid-string case returns only itself.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Mygate.

OA at Mygate?
Invisible during screen share
Get it