20. Valid Parentheses
easyAsked at PinterestValid Parentheses is a Pinterest phone-screen staple: given a string of brackets, determine whether they are balanced. The interviewer is checking that you reach for a stack the moment you see nested matching, not nested loops.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Pinterest loops.
- Glassdoor (2026-Q1)— Pinterest SWE phone-screen reports list Valid Parentheses as the warm-up before a stack-flavored follow-up.
- LeetCode Pinterest tag (2026-Q1)— On the Pinterest company-tagged problem list as a foundational warm-up.
Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input is valid if open brackets are closed by the same type of brackets, open brackets are closed in the correct order, and every closing bracket has a corresponding open bracket of the same type.
Constraints
1 <= s.length <= 10^4s consists of parentheses only '()[]{}'.
Examples
Example 1
s = '()'trueExample 2
s = '()[]{}'trueExample 3
s = '(]'falseExample 4
s = '([)]'falseExplanation: Brackets must close in LIFO order — interleaving is not allowed.
Approaches
1. Repeated string replacement (brute force)
Repeatedly replace innermost matched pairs '()', '[]', '{}' with empty string until no more replacements happen. If the result is empty, the input was balanced.
- Time
- O(n^2) worst case
- Space
- O(n)
function isValidBrute(s) {
let prev;
do {
prev = s;
s = s.replace('()', '').replace('[]', '').replace('{}', '');
} while (s !== prev);
return s.length === 0;
}Tradeoff: Cute but inefficient: each replace pass is O(n) and we may do n/2 passes. Mention it to anchor the conversation, then move to the stack.
2. Stack with pair lookup (optimal)
Push each opener onto a stack. On a closer, pop the top and verify it matches. Empty stack at the end means balanced.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const pairs = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const ch of s) {
if (ch === '(' || ch === '[' || ch === '{') {
stack.push(ch);
} else {
if (stack.pop() !== pairs[ch]) return false;
}
}
return stack.length === 0;
}Tradeoff: O(n) one-pass; a stack is the canonical data structure for matched nesting. The pair-lookup map keeps the dispatch table out of the if-else chain.
Pinterest-specific tips
Pinterest interviewers grade Valid Parentheses on three things: (1) you reach for a stack immediately; (2) you remember that an empty stack at end is required (not just that no mismatch fired); (3) you handle the edge of an unexpected closer with an empty stack — stack.pop() returns undefined, which fails the equality check, but call that out explicitly. The pair-lookup map is the stylistic move that signals seniority.
Common mistakes
- Returning true on first scan-complete without checking stack.length === 0 — leaves dangling openers.
- Forgetting that stack.pop() on an empty stack returns undefined — works by accident with the !== check but worth narrating.
- Using a counter per bracket type instead of a stack — counters don't catch '([)]'.
- Mutating the input string instead of using a stack — O(n^2).
Follow-up questions
An interviewer at Pinterest may pivot to one of these next:
- What if the string can contain other characters that should be ignored?
- Return the position of the first mismatch, not just a boolean.
- Generate all valid combinations of N pairs (LeetCode 22: Generate Parentheses).
- Longest valid parentheses substring (LeetCode 32) — harder DP follow-up.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Pinterest ask this in 2026?
It's a quick coding-aptitude signal. Most candidates pass; the ones who fail tip-off concerning coding fundamentals.
Is the pair-lookup map version expected, or is a switch-statement fine?
Either passes correctness. The map version is shorter and signals a slightly more polished style — worth the few extra seconds to type.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other Pinterest interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →