Generate Parentheses (Well-formed Combinations)?
Short answer: Follow on: List<string> GenerateParenthesis(int n) if (open < max) Generate(current + "(", open + 1, close, max, result); if (close < open) Generate(current + ")", open, close + 1, max, result); } Explanation: Use backtracking to add '(' and ')' only when valid.
Example code
{
List<string> result = new List<string>(); Generate("", 0, 0, n, result); return result;
} void Generate(string current, int open, int close, int max, List<string> result)
{
if (current.Length == max * 2)
{ result.Add(current); return;
}
Real-world example (ShopNest)
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png