Balanced Parentheses (Generative)

Data Structures
Medium
Google
85.7K views

Given $n$ pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Use a Backtracking approach and track the counts of open and close brackets.

Why Interviewers Ask This

Google interviewers use this problem to evaluate a candidate's mastery of recursion, backtracking logic, and constraint management. It tests the ability to visualize state transitions (open vs. close counts) without relying on brute force. Specifically, they assess whether you can prune invalid branches early to optimize time complexity, a critical skill for building scalable systems.

How to Answer This Question

1. Clarify Constraints: Immediately define input limits and edge cases like n=0 or n=1. State that we need all valid permutations, not just one. 2. Define the State: Explain that your recursive function needs three parameters: current string builder, count of open brackets used, and count of close brackets used. 3. Establish Base Case: Describe stopping when the string length equals 2*n, indicating a complete valid combination. 4. Formulate Recursive Logic: Detail the two rules: add an open bracket if open_count < n, and add a close bracket only if close_count < open_count. Emphasize that this second rule prevents malformed strings. 5. Optimize & Discuss Complexity: Mention using a StringBuilder or char array for O(1) append operations rather than string concatenation. Conclude by analyzing Time Complexity as O(4^n / sqrt(n)) and Space Complexity as O(n) for the recursion stack.

Key Points to Cover

  • Explicitly defining the pruning condition where close_count must be less than open_count
  • Demonstrating understanding of Time Complexity relative to Catalan numbers
  • Using mutable data structures like StringBuilder to avoid unnecessary object creation
  • Clearly articulating the base case and termination conditions of the recursion
  • Handling edge cases such as n=0 or negative inputs gracefully

Sample Answer

To solve the balanced parentheses problem efficiently, I would use a backtracking approach with a recursive helper function. The core idea is to build the solution character by character while maintaining strict constrai…

Common Mistakes to Avoid

  • Attempting to generate all 2^(2n) combinations first and then filtering them, which causes TLE due to lack of pruning
  • Failing to check if close_count is less than open_count before adding a closing bracket, leading to invalid outputs
  • Using string concatenation inside the recursive loop instead of a StringBuilder, resulting in poor performance
  • Neglecting to pass state variables correctly between recursive calls, causing infinite loops or incorrect counts

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 145 Google questions