2. Valid Parentheses
easyAsked at RedditDetermine if a string of brackets is balanced. Reddit uses this to test stack intuition — the same data structure that backs their comment-tree depth tracking and markdown parser for self-post bodies.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit backend phone screen, used to gate stack/queue intuition.
- Reddit r/cscareerquestions (2025-09)— Mentioned alongside markdown-parser follow-ups for Reddit.
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 = "(]"falseApproaches
1. Repeated string replace
Keep replacing '()', '[]', '{}' with empty string until the string stops shrinking.
- Time
- O(n^2)
- Space
- O(n)
function isValid(s) {
let prev;
while (prev !== s) {
prev = s;
s = s.replace('()', '').replace('[]', '').replace('{}', '');
}
return s === '';
}Tradeoff: Works but quadratic because each replace rescans the string.
2. Stack of expected closers (optimal)
Push the expected closing bracket on each open. On a 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 ch of s) {
if (pairs[ch]) {
stack.push(pairs[ch]);
} else if (stack.pop() !== ch) {
return false;
}
}
return stack.length === 0;
}Tradeoff: Single pass, O(n) time. Same pattern Reddit's markdown renderer uses to track nested code blocks and quotes.
Reddit-specific tips
Reddit values clean code over clever code. State the invariant explicitly: 'the stack always holds the closer we are currently waiting for.' Bonus signal: connect it to the comment-renderer stack that closes <blockquote> tags when nesting drops.
Common mistakes
- Returning true without checking that the stack is empty at the end (handles '(((').
- Popping into a variable but never comparing it to the current char.
- Forgetting to handle odd-length strings (early exit optimization).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Minimum number of insertions to balance (LC 1541).
- Longest valid parentheses substring (LC 32).
- Generate all valid parentheses of length 2n (LC 22).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why push the closer instead of the opener?
Saves a lookup. When we hit a closer, we compare directly to the top of the stack instead of mapping opener->closer.
When would this fail in production?
If you don't reset between calls or if the stack is shared (Reddit's markdown parser had a bug in 2014 where comment edits reused the stack).
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →