Stacks for Matching Brackets
Validate parentheses with a stack.
Stacks for Matching Brackets is a free Coding Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Last In, First Out
A stack is a pile where the last item you add is the first one you remove, just like stacking plates. 🍽️
Python Lists Are Stacks
In Python you do not need a special class. A plain list already works as a fast, ready-made stack for contests.
stack = []Push with append
To add an item on top of the stack you call append, which puts the value at the end of the list in O(1) time.
stack.append('(')
stack.append('[')Pop from the Top
Calling pop with no index removes and returns the last item, the one most recently pushed onto the stack.
top = stack.pop() # removes '['Peek Without Removing
To look at the top item without taking it off, just read stack[-1]. This peek is handy before deciding to pop.
if stack:
top = stack[-1]Always Check for Empty
Popping an empty stack raises an error. Guard every pop by first checking if stack so your solution never crashes.
The Bracket Matching Idea
Brackets nest perfectly, which screams stack. Push every opening bracket, and a closing one must match the top of the stack.
Map Closing to Opening
Keep a small dictionary that pairs each closing bracket with the opening one it expects, so checks stay clean.
pairs = {')': '(', ']': '[', '}': '{'}Scan and Decide
Walk the string once. Push openers, and on a closer compare it with the popped top using your pairs map.
for c in s:
if c in pairs.values():
stack.append(c)Mismatch Means Invalid
If the popped opener does not match, or the stack is empty when you need it, the string is invalid right away.
elif not stack or stack.pop() != pairs[c]:
return FalseEmpty Stack at the End
After scanning, a leftover opener means something never closed. The string is valid only when the stack is finally empty.
return not stackQuick Check
You are validating brackets with a stack. What does a non-empty stack at the very end tell you?
Recap: Stacks Tame Brackets
You learned that a list works as a stack: push openers, pop on closers, and an empty stack at the end means balanced. Nicely done! 🎉
Frequently asked questions
Is the “Stacks for Matching Brackets” lesson free?
Yes — the full text of “Stacks for Matching Brackets” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Stacks for Matching Brackets”?
Validate parentheses with a stack. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Stacks for Matching Brackets” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Stacks for Matching Brackets
- Monotonic Stack: Next Greater Element
- Queues and collections.deque
- Sliding Window Maximum with Deque