FastPrepMinimum Insertions to Balance Parentheses
Problems/Minimum Insertions to Balance Parentheses

Minimum Insertions to Balance Parentheses

Google logoGoogle● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand new-grad report from Google asks for the fewest parentheses you must insert to make a parenthesis sequence valid. This is the standard version of that well-known problem. We chose how empty strings work and a finite size limit. Our version is about 96% the same as the reported question.

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) → int

Examples

Example 1

s = "())"return = 1

Insert one opening parenthesis before the final closing parenthesis.

Example 2

s = "((("return = 3

Each unmatched opening parenthesis needs a closing parenthesis.

Example 3

s = "()"return = 0

The string is already valid.

Constraints

  • 0 <= s.length <= 100000.
  • s contains 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int minInsertionsToBalance(String s) {
        // Write your code here.
    }
}
Checking account…