Generate Balanced Parentheses
Updated
Problem statement
Given an integer n, return every well-formed string that can be made with exactly n pairs of parentheses.
A string is well-formed when each opening parenthesis ( is matched with a later closing parenthesis ), and no prefix contains more closing parentheses than opening parentheses.
Return the strings in ascending lexicographic (dictionary) order. With the usual character order, ( comes before ).
Function
generateBalancedParentheses(n: int) → List<String>Examples
Example 1
n = 3return = ["((()))","(()())","(())()","()(())","()()()"]These are all five well-formed strings containing exactly three opening and three closing parentheses, listed in lexicographic order.
Example 2
n = 2return = ["(())","()()"]The two well-formed strings with two pairs are (()) and ()(), in lexicographic order.
Example 3
n = 1return = ["()"]Only one well-formed string can be made with one pair of parentheses.
Constraints
1 ≤ n ≤ 10- The returned list must be in ascending lexicographic order.
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Delivery ServiceOA · Seen Jun 2026
- Shared InterestOA · Seen Jun 2026
Similar questions at other companies
- DFS Subsequences of a String (For MLE)Wayfair · OAWayfair interview questions
- Permutations of Unique CharactersSalesforce · PHONE SCREENSalesforce interview questions
- Phone Keypad Letter CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
- Permutations of Character PositionsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- All Rearrangements of a StringAdobe · PHONE SCREENAdobe interview questions
- DFS Subsequences of a String (For MLE)CoinDCX · PHONE SCREEN
- Expression Add OperatorsPinterest · PHONE SCREENPinterest interview questions
- Next Number with Disjoint DigitsElevenLabs · OAElevenLabs interview questions
- Decode StringAbridge · OAAbridge interview questions
- String Permutations in Custom Character OrderWex · OA · PHONE SCREEN