FastPrepRemove Invalid Parentheses
Problems/Remove Invalid Parentheses

Remove Invalid Parentheses

Mygate logoMygate● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Mygate onsite interview for a full-time role. The report names removing parentheses but leaves out whether to remove the minimum, and whether to return one result or all. Here you return all unique results that use the minimum number of removals.

Our version is about 75% the same as the reported question.

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 different string you can make using the fewest 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.

More Mygate interview questions

  • Issue Non-repeating OTPs in a Time WindowONSITE INTERVIEW · Seen Aug 2019
  • Employees Earning More Than Their ManagerPHONE SCREEN · Seen Sep 2026
  • Maximum Subarray SumPHONE SCREEN · Seen Sep 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Sep 2026
  • Users Active in Both January and FebruaryPHONE SCREEN · Seen Sep 2026
  • Elements Occurring More Than n/3 TimesPHONE SCREEN · Seen Jan 2025
  • Frog Jump: Minimum EnergyPHONE SCREEN · Seen Jan 2025
  • Left View of a Binary TreePHONE SCREEN · Seen Jan 2025
  • Two SumPHONE SCREEN · Seen Aug 2024
  • Minimum in a Rotated Sorted ArrayPHONE SCREEN · Seen Jul 2024

Similar questions at other companies

  • Word Ladder IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
  • All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions
  • Word LadderGoogle · ONSITE INTERVIEWGoogle interview questions
  • Two-Tower Strategy GameJane Street · ONSITE INTERVIEWJane Street interview questions
  • Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
  • Minimum Word Ladder LengthSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
All Mygate interview questionsSee Mygate hiring insights
Saved
class Solution {
    public List<String> removeInvalidParentheses(String s) {
        // write your code here
    }
}
Checking account…