Can a Template With Wildcard Characters Be Bracket Balanced

Last updated: November 29, 2026

Quick Overview

A Docs template contains '(', ')', and '*' where each '*' may become '(', ')', or nothing. Return whether some replacement makes the bracket string balanced.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A Docs template contains '(', ')', and '*' where each '*' may become '(', ')', or nothing. Return whether some replacement makes the bracket string balanced.

Google interviewers use this to see if a candidate can replace a branching search with a range of possible states. The low/high open-count greedy is the expected answer, and the interviewer checks the clamping step carefully.

What the Interviewer Expects
  • Recognize that only the count of open brackets matters
  • Track the minimum and maximum possible open count
  • Fail when the maximum goes negative
  • Clamp the minimum at zero
  • Accept when the minimum is zero at the end
Key Topics to Cover
Greedy
Range tracking
Parentheses
State compression
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 valid replacement.
  • Wildcards can be '(' or ')' but not empty.
  • Count how many replacements are valid (DP).
  • Three bracket types with wildcards: does the range trick still work?
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 over (, ), *, length up to 10^4. Output: True if some assignment of each * makes the string balanced....

High-Level Design

Instead of exploring 3^k assignments, track the set of reachable open counts. That set is always a contiguous range [lo, hi]. On ( both bounds rise. On ) both fall. On * the range widens: `lo ...


Submit Your Answer
Markdown supported