2. Valid Parentheses
easyAsked at NubankValidate that nested bracket structures in a KYC document parser are balanced.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Brackets must close in the correct order and every open bracket needs a matching close.
Constraints
1 <= s.length <= 10^4s consists of parentheses only
Examples
Example 1
s="()[]{}"trueExample 2
s="(]"falseApproaches
1. Brute force
Repeatedly remove matched 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; on closer, pop and compare. Empty stack at end means valid.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const stack = [];
const pairs = { ')': '(', ']': '[', '}': '{' };
for (const c of s) {
if (c in pairs) {
if (stack.pop() !== pairs[c]) return false;
} else stack.push(c);
}
return stack.length === 0;
}Tradeoff:
Nubank-specific tips
Nubank uses parser-style problems to probe whether you can reason about malformed third-party KYC payloads without crashing the ingestion pipeline.
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 Nubank interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →