Microsoft · Question 26 · Stacks
Valid Parentheses
Problem
Given a string containing only '(', ')', '{', '}', '[' and ']', determine if the input string is valid. A string is valid if open brackets are closed in the correct order.
Approach
Push expected closers; each closer must match the stack top.
Time O(n)Space O(n)
How it works
- 1Push the expected closer for each opener.
- 2Mismatch or leftover means invalid.
Sample input / output
Input:
()[]{}
Output:
trueConstraints
1 <= s.length <= 10^4; brackets only.
Solution — read it, then walk it
Time O(n)Space O(n)Loops 1Branches 4Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/15
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits