Fewest Brackets to Insert So a Sheets Formula Balances

Last updated: November 29, 2026

Quick Overview

A Google Sheets formula string contains only round brackets. Return the minimum number of '(' or ')' characters you must insert anywhere so the brackets are balanced.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A Google Sheets formula string contains only round brackets. Return the minimum number of '(' or ')' characters you must insert anywhere so the brackets are balanced.

A warm-up in Google phone screens that checks you can replace a stack with a counter and reason about two different kinds of imbalance. The interviewer also likes asking what the counts mean, not just the code.

What the Interviewer Expects
  • Track open brackets awaiting a match
  • Count unmatched closing brackets separately
  • Return the sum of both imbalances
  • Explain why inserting is always enough and minimal
  • O(n) time, O(1) space
Key Topics to Cover
Greedy
Counting
Parentheses
Strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return one balanced string achieving the minimum.
  • Support three bracket types.
  • Deletions only instead of insertions; what is the minimum and is it the same number?
  • Brackets inside string literals in the formula should be ignored.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: string of ( and ). Output: minimum insertions to make it balanced, meaning every prefix has at least as many opens as closes and the totals match....

High-Level Design

Scan with open_count, the number of ( not yet matched. On ( increment it. On ), match with an open one if available; otherwise this ) can never be matched by anything after it, so we must in...


Submit Your Answer
Markdown supported