Practice: Generate Parentheses
Problem: https://leetcode.com/problems/generate-parentheses/
Recognition reminder: you must build every well-formed combination of n pairs of parentheses. This is a different flavor of stack: the stack is the recursion’s own call stack, and the validity rule (you can only close a bracket that is still open) is what keeps the search inside the legal strings.
Before you start (the five-beat rhythm)
- Your Pattern Card for this week is already written.
- Name the pattern aloud and write your approach as a plain-English comment before any code.
- Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
- If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
- Debrief in your commit message before moving to the next problem.
Your target
Fill these in yourself before you look at anyone else’s solution:
Target time complexity: ____
Target space complexity: ____
The count of valid strings for n pairs is the nth Catalan number, so any correct method is at least that expensive; do not expect to beat it. The skill is the rule that lets you build only valid strings instead of generating all 2^(2n) strings and filtering. Ask: at any point in building a string, when is it legal to add an open bracket, and when is it legal to add a close bracket? Those two conditions are the whole solution.
Where your code goes
Write your solution as a function generate_parenthesis(n: int) -> list[str] in a file named solution.py in your own work repo (see getting-started.md), not in the course repo. (Note the function name is singular, generate_parenthesis, matching LeetCode, even though the folder is plural.) The strings may be returned in any order. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.
Debrief (paste into your commit message)
1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?