Valid Parentheses With a Stack
Step by Step
Valid parentheses pushes every open bracket. Every close bracket must match the top, and the stack must end empty.
Step through every bracket in both directions, then share a link to the step where the match failed.
What You Watch While Brackets Push and Pop
The stack, the top bracket and the match result change on every step.
Brackets close in the opposite order they open. Stack order remembers that.
CodeStory's visualiser shows the stack growing and popping with the item on top highlighted. Marked code lines follow every move, and you can step back, step forward or play.
Open Crossed pairs first. Then load Left open to see why the final check matters.
How Valid Parentheses Matches Every Bracket
Steps below follow the stack on your screen.
- 1Move through the string from left to right.
- 2Push every open bracket onto the stack.
- 3Pop the top when a close bracket arrives, and stop if the two do not match.
- 4End with an empty stack. Anything left over means an open bracket never closed.
Valid Parentheses Mistakes the Visualiser Shows
A switch in the visualiser turns the mistake on, so you can watch the state go wrong.
- Bug: no final checkReturns true at the end without looking at the stack, so brackets that were opened and never closed slip through. Load Left open to see it.
Valid Parentheses Inputs Worth Trying
Three presets cover crossed pairs, open brackets and a single bracket.
- Crossed pairsEvery bracket has a partner of its kind, but the pairs cross.
- Left openEvery close matches, but two opens never close. Switch on "Bug: no final check" here.
- One bracketSwitch on the no-final-check bug here too.
Valid Parentheses Cost in Time and Space
Every bracket is pushed once and popped once, so the work stays linear.
- Time
- O(n)counted from the pushes and pops you step through
- Space
- O(n)the stack that holds open brackets
- Pattern
- StackThe last open bracket is the first one to close.
Practise Stacks Beyond Parentheses
Watching shows the stack and solving trains you to match pairs in order.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
Sign up for 100 AI credits now and get 100 more every month. Start free in the app
Valid Parentheses Visualiser FAQs
Open the visualiser and follow the stack bracket by bracket.
Open the visualiserWhat does the valid parentheses visualiser show?
CodeStory's visualiser shows the stack growing and popping with the item on top highlighted, beside the highlighted code. Move back and forth through every step, or let it play.
What is the time complexity of valid parentheses?
O(n) time and O(n) space. Live counts show one push and one pop per bracket.
Why does my valid parentheses code fail on some inputs?
Skipping the final stack check is a common cause. Switch the bug on in the visualiser and watch open brackets slip through.
Can I use my own input?
Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.
Is the visualiser free?
Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.
Stuck on Stacks? Get a Free Callback
Tell CodeStory which stack step loses you, and a mentor plans what comes next.
- 1CodeStory checks your level, from stack basics to interview ready.
- 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
- 3You decide. No pressure, no payment on the call.