Generate All Balanced Bracket Strings for a Parser Fuzzer
Last updated: November 29, 2026
Quick Overview
Given n, generate every string of n pairs of parentheses that is balanced, for use as structured test input to a parser fuzzing harness.
Microsoft
November 29, 20260
0
0 solved
Given n, generate every string of n pairs of parentheses that is balanced, for use as structured test input to a parser fuzzing harness.
A Microsoft phone screen backtracking question, framed here around generating valid inputs for fuzz testing a compiler front end, something teams working on C#, TypeScript and the Windows toolchain care about. The interviewer checks that you prune invalid prefixes instead of generating all 2^(2n) strings and filtering.
What the Interviewer Expects
- Track counts of open and close brackets used
- Only add a close bracket when it would not go negative
- Build the string incrementally and undo after recursing
- Explain the Catalan-number output size
- Contrast with generate-and-filter
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you generate strings with three bracket types, each balanced and properly nested?
- How would you generate a uniformly random balanced string without enumerating all of them?
- Can you write it iteratively?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Return all distinct balanced strings of length 2n made of ( and ). Balanced means every prefix has at least as many opens as closes and the totals are equal. n is small (up to about 12), because t...
High-Level Design
Build strings left to right and keep only prefixes that can still become balanced. Two counters capture everything: opened and closed. An open bracket is legal while opened < n. A close bracket ...