Minimum Insertions to Balance Parentheses
Updated
Problem statement
Given a string s containing only '(' and ')', return the minimum number of parentheses that must be inserted at any positions to make the string valid.
A valid parenthesis string has every opening parenthesis matched with a later closing parenthesis, and every closing parenthesis matched with an earlier opening parenthesis.
Function
minInsertionsToBalance(s: String) → intExamples
Example 1
s = "())"return = 1Insert one opening parenthesis before the final closing parenthesis.
Example 2
s = "((("return = 3Each unmatched opening parenthesis needs a closing parenthesis.
Example 3
s = "()"return = 0The string is already valid.
Constraints
0 <= s.length <= 100000.scontains only'('and')'.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Maximize Binary XOR with a Set-Bit LimitJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Wrap Text with Retained Boundary SpacesAtlassian · PHONE SCREENAtlassian interview questions
- Palindrome After At Most One DeletionAdobe · PHONE SCREENAdobe interview questions
- Wrap Words from Existing Text LinesCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Remove K DigitsProcol · PHONE SCREEN
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Valid Parenthesis StringOracle · PHONE SCREENOracle interview questions