Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Generate Parentheses
Problem
Given an integer n, return every string made of exactly n opening and n closing parentheses that is well-formed, meaning every closing bracket matches an earlier unmatched opening one. Output order does not matter.
- n is between 1 and 9 pairs.
Examples
n = 3→["((()))","(()())","(())()","()(())","()()()"]. Five strings;"())(()"is excluded because the third character closes with nothing open.n = 1→["()"].n = 2→["(())","()()"].
Hints
- Build the string left to right. At any moment, what two counts fully describe whether the prefix can still be completed?
- You may add
(as long as you have some left; you may add)only if it would match an open bracket that is still unmatched. - Backtracking with
open_usedandclose_usedcounters; add)only whenclose_used < open_used.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.