FastPrepMinimum Remove to Make Valid Parentheses
Problems/Minimum Remove to Make Valid Parentheses

Minimum Remove to Make Valid Parentheses

WhatNot logoWhatNot● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A new grad met this at Whatnot in late 2024, as the first of two questions in the hour-long technical round that followed the Karat screen. The write-up calls it an easier take on the well-known task of deleting the fewest brackets to balance a string.

It does not say what was easier, so here you get the full standard task: lowercase letters plus round brackets, up to 10^5 characters. Since many strings can be correct, the statement fixes one pairing rule so every input has a single answer.

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

You are given a string s made of the characters '(', ')' and lowercase English letters.

Remove as few parentheses as possible so that the parentheses left in the string are valid. Letters are never removed, and the remaining characters keep their order. The parentheses are valid when every '(' can be paired with a later ')' and every ')' with an earlier '(', each character used in only one pair, and no two pairs crossing. A string with no parentheses at all is valid.

Often several strings remove the same smallest number of parentheses. Return the one made by this rule: read s from left to right and pair each ')' with the nearest '(' before it that has no partner yet. Remove every ')' that finds no partner, and at the end remove every '(' that never got one.

Function

minRemoveToMakeValid(s: String) → String

Examples

Example 1

s = "a)b(c)d"return = "ab(c)d"

The ) right after a has no ( before it, so it is removed. The remaining pair around c is fine.

Example 2

s = "(a(b)"return = "a(b)"

The ) pairs with the nearest unpaired ( before it, the one just before b. The first ( is left without a partner and is removed. Removing the other ( instead would give "(ab)", which also removes one character, but the pairing rule picks "a(b)".

Example 3

s = "))(("return = ""

Neither ) has a ( before it, and neither ( has a ) after it. All four are removed, so the answer is the empty string.

Example 4

s = "x(y)z)("return = "x(y)z"

The pair around y stays. The ) after z has nothing left to pair with, and the final ( has no ) after it. Both are removed.

Example 5

s = "(ab)(c)"return = "(ab)(c)"

Every parenthesis already has a partner, so nothing is removed.

Constraints

  • 1 <= s.length <= 105
  • s[i] is '(', ')' or a lowercase English letter.

More WhatNot Onsite Interview questions

  • User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
  • Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
  • Number of Submatrices That Sum to TargetONSITE INTERVIEW · Seen Jul 2024
  • Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
  • Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
  • K Smallest Values in a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
  • Average Purchase PriceONSITE INTERVIEW · Seen Sep 2022

Similar questions at other companies

  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Number Words to Digits in Any LanguageMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
  • Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Inorder Tree String EquivalenceGoogle · ONSITE INTERVIEWGoogle interview questions
  • Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions

Interview reports that mention this question

  • Whatnot SWE New Grad Second Technical Interview: 8 Experiences

WhatNot interview experiences

  • Whatnot interviews · October 2026
  • Whatnot Experienced SWE Phone Screen Questions: 2 Experiences
  • Whatnot SWE Interview Questions: 6 Experiences
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public String minRemoveToMakeValid(String s) {
        // write your code here
    }
}
Checking account…