← Questions

Validate a bracket string after digit-driven deletions

0
GoogleCodingLast asked Aug 2025

A coding interview question asked in Google's onsite coding round, last asked Aug 2025. It tests stacks, greedy and strings.

A string contains only (, ) and the digits 0 to 9. Each digit at position i with value v must delete exactly v brackets that come before position i. A bracket can be deleted at most once, by one digit; digits are never deleted and are ignored when checking the result. Decide whether some choice of deletions leaves a valid bracket sequence.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • can_balance(s: str) -> bool. Optionally also return one witness: the positions to delete.

Example

can_balance("(())2)1()")  ->  True     # delete 0, 2, 3: "()()" remains
can_balance("(()1")       ->  True     # delete the first "(": "()" remains
can_balance("(()2)")      ->  True     # delete 0 and 2: "()" remains
can_balance(")(2()")      ->  True     # the 2 deletes both earlier brackets
can_balance("()1")        ->  False    # deleting one bracket leaves one
can_balance("1()")        ->  False    # nothing before the 1 to delete
can_balance("((2))")      ->  False    # deleting both "(" leaves "))"

Constraints

  • 0 <= len(s) <= 2000 for a DP over positions, deletions and balance; discuss larger inputs.

Clarifying questions

  • Must a digit's deletions come strictly before it?
  • Is a digit 0 allowed, and does it do anything?
  • Is the empty bracket sequence valid?
  • Do you want the deletions themselves, or only yes or no?

What a strong answer covers

  • The key reduction: what matters is not which digit deletes which bracket, only that by each digit the number of deleted earlier brackets is at least the sum of the digits so far, and that the total equals the sum of all digits (so nothing is deleted after the last digit).
  • A DP over (position, deleted so far, open balance): at a bracket, delete it or keep it (a kept ) needs a positive balance); at a digit, check the running requirement; at the end, balance 0 and deletions equal to the total. O(n^3) states in the worst case, with pruning.
  • Reconstructing a witness from the DP choices.
  • A greedy idea worth testing against the DP: which brackets are best to delete (unmatched ) first, then ( from the left?). Show a counterexample or argue it.
  • Tests: no digits, a digit with nothing before it, 0 digits, deletions that must be split across digits.

Follow-ups

  • Return the lexicographically smallest set of deleted positions.
  • Digits may also delete brackets after them.
  • Several bracket types (()[]{}).

Where it's asked

  • Google · Onsite coding · Last asked Aug 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

stacks · greedy · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding