← Amazon questions

Valid Bracket Combinations

Filed under Amazon

Free read 3 of 3

Use 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

  1. Use recursion to build the string. Maintain counts of remaining opening/closing brackets for each type.

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.