2. Valid Parentheses
easyAsked at ZoomDetermine if a string of brackets is properly nested and closed.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a string containing only '()[]{}', determine if the input is valid. Brackets must be closed in the correct order and each open bracket has a matching close.
Constraints
1 <= s.length <= 10^4s consists only of bracket characters
Examples
Example 1
s="()[]{}"trueExample 2
s="(]"falseApproaches
1. Repeated replace
Repeatedly remove '()', '[]', '{}'.
- Time
- O(n^2)
- Space
- O(n)
while (s.includes('()')||s.includes('[]')||s.includes('{}'))
s = s.replace('()','').replace('[]','').replace('{}','');
return s==='';Tradeoff:
2. Stack
Push opens, pop on close and check match. Empty stack at the end means valid.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const map = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const ch of s) {
if (!(ch in map)) stack.push(ch);
else if (stack.pop() !== map[ch]) return false;
}
return stack.length === 0;
}Tradeoff:
Zoom-specific tips
Zoom interviewers often ask candidates to extend this into protocol-frame validation (RTP/SIP packet bracket-like delimiters), so anchor your stack analogy to streaming-protocol parsing.
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 Zoom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →