PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Eightfoldai

Make a Parentheses String Valid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in string manipulation and delimiter matching, specifically balancing parentheses while preserving character order and handling edge cases.

  • medium
  • Eightfoldai
  • Coding & Algorithms
  • Software Engineer

Make a Parentheses String Valid

Company: Eightfoldai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string `s` containing lowercase English letters and the characters `'('` and `')'`, remove the minimum number of parentheses so that the resulting string is valid. A string is considered valid if: - every `')'` has a matching `'('` before it, and - the total number of `'('` and `')'` is balanced. You must preserve the relative order of all remaining characters. Return **any** valid string that can be obtained after the minimum number of removals. Examples: - Input: `"lee(t(c)o)de)"` -> Output: `"lee(t(c)o)de"` - Input: `"a)b(c)d"` -> Output: `"ab(c)d"` - Input: `"))("` -> Output: `""` - Input: `"())()("` -> Output: `"()()"` or another valid minimum-removal result Possible follow-ups: 1. Return only the minimum number of removals. 2. If multiple optimal answers exist, return the lexicographically smallest valid result. 3. Discuss whether the extra space can be reduced.

Quick Answer: This question evaluates proficiency in string manipulation and delimiter matching, specifically balancing parentheses while preserving character order and handling edge cases.

Eightfoldai logo
Eightfoldai
Mar 19, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
0

Given a string s containing lowercase English letters and the characters '(' and ')', remove the minimum number of parentheses so that the resulting string is valid.

A string is considered valid if:

  • every ')' has a matching '(' before it, and
  • the total number of '(' and ')' is balanced.

You must preserve the relative order of all remaining characters. Return any valid string that can be obtained after the minimum number of removals.

Examples:

  • Input: "lee(t(c)o)de)" -> Output: "lee(t(c)o)de"
  • Input: "a)b(c)d" -> Output: "ab(c)d"
  • Input: "))(" -> Output: ""
  • Input: "())()(" -> Output: "()()" or another valid minimum-removal result

Possible follow-ups:

  1. Return only the minimum number of removals.
  2. If multiple optimal answers exist, return the lexicographically smallest valid result.
  3. Discuss whether the extra space can be reduced.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Eightfoldai•More Software Engineer•Eightfoldai Software Engineer•Eightfoldai Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.