2. Valid Parentheses
easyAsked at IndeedValidate that bracket pairs are properly nested and closed — Indeed uses it to gauge stack fluency before resume-parser and search-query syntax problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input is valid when brackets are closed by the same type and in the right order.
Constraints
1 <= s.length <= 10^4s consists of bracket characters only
Examples
Example 1
s = "()[]{}"trueExample 2
s = "(]"falseApproaches
1. Brute force replace
Repeatedly strip matching pairs until none remain.
- Time
- O(n^2)
- Space
- O(n)
while (s.includes('()') || s.includes('[]') || s.includes('{}')) {
s = s.replace('()','').replace('[]','').replace('{}','');
}
return s.length === 0;Tradeoff:
2. Stack
Push openers and pop on closers, checking the top matches the expected pair.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const pair = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const c of s) {
if (!pair[c]) stack.push(c);
else if (stack.pop() !== pair[c]) return false;
}
return stack.length === 0;
}Tradeoff:
Indeed-specific tips
Indeed wants the stack-popping logic mapped to validating search-query operators (parens around AND/OR clauses) — mention that connection during your walkthrough.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other Indeed interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →