2. Valid Parentheses
easyAsked at IntuitGiven a string containing brackets, determine if every opener has a matching closer in correct order. Intuit asks this to test stack basics — relevant because TurboTax and QuickBooks parse nested form-rule expressions and reconciliation grouping all day.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Intuit loops.
- Glassdoor (2026-Q1)— Intuit screen at TurboTax org, framed in context of tax-form expression validation.
- Reddit r/cscareerquestions (2025-09)— QuickBooks new-grad screen reportedly opened with Valid Parentheses.
Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if open brackets are closed by the same type of brackets and open brackets are closed in the correct order.
Constraints
1 <= s.length <= 10^4s consists of parentheses only '()[]{}'.
Examples
Example 1
s = "()"trueExample 2
s = "()[]{}"trueExample 3
s = "(]"falseExplanation: Closing bracket doesn't match the most recent opener.
Approaches
1. Repeated string replacement
Keep replacing '()', '{}', '[]' with empty string until the string stops changing.
- Time
- O(n^2)
- Space
- O(n)
function isValid(s) {
let prev;
do { prev = s; s = s.replace('()','').replace('{}','').replace('[]',''); } while (prev !== s);
return s.length === 0;
}Tradeoff: Quadratic; allocates new strings each iteration. Show only as the anti-pattern.
2. Single-pass stack (optimal)
Walk through s. Push openers onto a stack. On a closer, pop and check it matches the expected pair. At the end the stack must be empty.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const pair = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const ch of s) {
if (ch === '(' || ch === '[' || ch === '{') {
stack.push(ch);
} else {
if (stack.pop() !== pair[ch]) return false;
}
}
return stack.length === 0;
}Tradeoff: Single pass, O(n). The empty-stack check at the end catches strings like '(' (unclosed opener).
Intuit-specific tips
Intuit interviewers often follow up by asking how you'd extend this to validate nested tax-form rule expressions (e.g., IF/THEN/ELSE blocks in TurboTax). Articulate that the same stack pattern generalizes to any LIFO matching. Bonus signal: handle the early-exit case where a closer arrives with an empty stack (return false immediately, don't crash).
Common mistakes
- Forgetting the final stack.length === 0 check — '(((' would incorrectly return true.
- Popping from an empty stack without guarding — throws on input like ')'.
- Using a regex .replace loop without checking for fixed-point — infinite loop on inputs with no pairs.
Follow-up questions
An interviewer at Intuit may pivot to one of these next:
- Minimum number of insertions to make the string valid (LC 921).
- Longest Valid Parentheses (LC 32).
- Validate a more general grammar with operator precedence.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
What if the string contains other characters mixed in?
The constraint says bracket-only, but in real code you'd skip non-bracket chars or reject them depending on the spec. Always clarify with the interviewer.
Can I solve this with a counter instead of a stack?
Only if there's a single bracket type. With three types you need a stack because '([)]' must be invalid — a counter wouldn't catch that.
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other Intuit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →