Valid Bracket Combinations
Filed under Amazon
Free read 3 of 3Use recursion to build the string. Maintain counts of remaining opening/closing brackets for each type.
The question
Given n1 pairs of {}, n2 pairs of [], and n3 pairs of (), generate all valid combinations of these brackets.
Constraints
- n1, n2, n3 >= 0.
- Total length is 2 * (n1 + n2 + n3).
- Must maintain correct nesting order.
Approach
Use recursion to build the string. Maintain counts of remaining opening/closing brackets for each type.
Maintain a stack of opening brackets to ensure the next closing bracket matches the top of the stack.
When all brackets are used, add the current string to the result list.
Tips
- What they are testing: Test with zero pairs and single pair types.
- Common mistake: Failing to track the stack state to ensure the closing bracket matches the most recent opening bracket.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.