Generate Parentheses
Updated
Problem statement
Given n pairs of parentheses, return every well-formed sequence containing exactly those pairs.
Return the sequences in lexicographic order.
Function
generateParenthesis(n: int) → List<String>Examples
Example 1
n = 3return = ["((()))","(()())","(())()","()(())","()()()"]These are all five balanced sequences using three pairs, ordered lexicographically.
Example 2
n = 4return = ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]With four pairs there are 14 well-formed sequences. They are listed in lexicographic order, where ( comes before ).
Example 3
n = 1return = ["()"]One pair can only form ().
Constraints
1 <= n <= 8
More Amazon Onsite Interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Generate Strings of Length at Least ThreeMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Insert Addition and Multiplication OperatorsPinterest · ONSITE INTERVIEWPinterest interview questions
- Enumerate All SubsequencesInfosys · ONSITE INTERVIEWInfosys interview questions
- Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
- Generate All Valid Word AbbreviationsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Phone Keypad Letter CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
- Generate ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Generate Palindromic PermutationsMotive · ONSITE INTERVIEWMotive interview questions
- Permutations of Character PositionsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Letter Combinations of Phone DigitsCapital One · ONSITE INTERVIEWCapital One interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)