Every balanced string of brackets
A string of ( and ) is balanced when it has as many of each and, reading left to right, the closing brackets never outnumber the opening ones so far.
Return every balanced string with exactly n opening and n closing brackets, in sorted order, where ( comes before ).
- Input
n = 2- Output
["(())","()()"]
Of the six strings with two of each bracket, only these two never close more than they have opened.
- Input
n = 1- Output
["()"]
With one pair there is only one way.
Limits
1 <= n <= 9
Hints
Hint 1
Build the string one bracket at a time. You may add ( while fewer than n are open, and ) while there are fewer closes than opens.
Hint 2
Trying ( before ) at every step produces the strings already in sorted order.
Where this was reported
- Zoho, SDE campus interview, 2025 (Medium)
- Amazon, SDE-1 fresher interview, 2025 (LeetCode)
- Flipkart, SDE-1 off-campus interview, 2024 (Hashnode)
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.

