Free beta: free to use with daily limits while we build. Join the waitlist for founding-member pricing at launch. Join the waitlist

Sign up free

Whatnot interview question

Remove the fewest parentheses to make a string balanced

Company:
Whatnot
Round:
Technical
Type:
Coding
Difficulty:
Medium
Reported:
June 2, 2026

Problem

Given a string s of lowercase letters and the characters ( and ), remove the minimum number of parentheses so that the result is balanced, and return the result. Letters are never removed. A string is balanced if every ) closes an earlier unmatched ( and no ( is left unmatched.

When several answers exist, keep the leftmost possible opening parentheses and remove unmatched closing ones as they appear (the greedy result described below).

Example 1

Input: s = "ab(c(d)e)f)"
Output: "ab(c(d)e)f"

The final ) has no matching (, so it is removed.

Example 2

Input: s = "(a(b"
Output: "ab"

Both opening parentheses are unmatched.

Constraints

  • 1 <= len(s) <= 10^5
  • s contains only lowercase letters, ( and )

Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern