Bird
Raised Fist0
Interview PrepbacktrackingmediumAmazonFacebookGoogleBloomberg

Generate Parentheses

Choose your preparation mode3 modes available
📋
Problem

Imagine you are designing a code editor that automatically suggests all valid ways to complete a partially typed expression with parentheses. Generating all valid combinations of parentheses helps ensure syntactically correct expressions.

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses strings. Return the list of all valid strings.

1 ≤ n ≤ 8
Edge cases: n = 1 → ["()"]n = 0 → [] (no pairs means no parentheses)n = 2 → ["(())", "()()"]
</>
IDE
def generateParenthesis(n: int) -> list:public List<String> generateParenthesis(int n)vector<string> generateParenthesis(int n)function generateParenthesis(n)
def generateParenthesis(n):
    # Write your solution here
    pass
class Solution {
    public List<String> generateParenthesis(int n) {
        // Write your solution here
        return new ArrayList<>();
    }
}
#include <vector>
#include <string>
using namespace std;

vector<string> generateParenthesis(int n) {
    // Write your solution here
    return {};
}
function generateParenthesis(n) {
    // Write your solution here
}
0/9
Common Bugs to Avoid
Wrong: ((()))(()())(())()()(())Missing last valid sequence '()()()' due to greedy always adding '(' first.In backtracking, explore adding ')' when close < open, not just '(' when open < n.
Wrong: ()()()()()((()))Output includes invalid sequences with more than n pairs due to reusing pairs multiple times.Limit open and close counts to n; do not reuse pairs beyond n.
Wrong: ((()))(()())(())()()(())()()()()Including sequences shorter than 2*n length due to incorrect recursion termination.Add sequences only when length == 2*n.
Wrong: ()Non-empty output for n=0 due to missing base case handling.Return empty list immediately if n=0.
Test Cases
t1_01basic
Input{"n":3}
Expected["((()))","(()())","(())()","()(())","()()()"]

All valid combinations of 3 pairs of parentheses are generated without duplicates.

t1_02basic
Input{"n":2}
Expected["(())","()()"]

All valid combinations of 2 pairs of parentheses are generated without duplicates.

t2_01edge
Input{"n":0}
Expected[""]

No pairs means no parentheses; output is empty list.

t2_02edge
Input{"n":1}
Expected["()"]

Single pair of parentheses has only one valid combination.

t2_03edge
Input{"n":8}
Expectednull

Largest input within constraints; must complete efficiently without timeout.

t3_01corner
Input{"n":3}
Expected["((()))","(()())","(())()","()(())","()()()"]

Tests greedy approach trap: adding '(' whenever possible leads to missing valid sequences.

t3_02corner
Input{"n":2}
Expected["(())","()()"]

Tests confusion between 0/1 knapsack style and this problem: cannot reuse parentheses pairs multiple times.

t3_03corner
Input{"n":4}
Expected["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]

Tests off-by-one errors in recursion depth or counts leading to missing or extra sequences.

t4_01performance
Input{"n":8}
⏱ Performance - must finish in 2000ms

n=8 tests performance of backtracking with pruning; brute force O(2^(2n)*n) will time out.