2. Valid Parentheses
easyAsked at SalesforceDetermine if a string of brackets is properly balanced. Salesforce uses this to verify you reach for a stack on nesting problems and can handle their SOQL-style query parsers.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce phone-screen for backend roles cites this as a parser warmup.
- Blind (2025-11)— Asked as a precursor to SOQL parser questions.
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, open brackets are closed in the correct order, and every close 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 = "(]"falseExplanation: Mismatched closing bracket type.
Approaches
1. Repeated substring replacement
Keep replacing '()', '[]', '{}' with empty string until no change; valid iff result is empty.
- Time
- O(n^2)
- Space
- O(n)
function isValid(s) {
let prev;
do {
prev = s;
s = s.replace('()', '').replace('[]', '').replace('{}', '');
} while (s !== prev);
return s.length === 0;
}Tradeoff: Cute but O(n^2). Salesforce will push you to a single pass.
2. Stack with pair map
Push opens; when you see a close, pop and verify the pair matches. Empty stack at end means valid.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const pair = { ')': '(', ']': '[', '}': '{' };
const stack = [];
for (const c of s) {
if (c === '(' || c === '[' || c === '{') {
stack.push(c);
} else {
if (stack.pop() !== pair[c]) return false;
}
}
return stack.length === 0;
}Tradeoff: Single pass, classic stack pattern. The pair-map trick keeps the dispatch O(1) and the code short.
Salesforce-specific tips
Salesforce sees this problem as a gateway into parser-style work — SOQL query parsing, Lightning Web Component template validation, and Apex code analysis all use the same stack-based pattern. Bonus signal: spontaneously mention how this generalizes to parsing nested data formats, since Salesforce's platform is built around them.
Common mistakes
- Returning true as soon as the stack empties mid-string — '())' would falsely return true.
- Forgetting to check stack.length === 0 at the end — '(((' returns true otherwise.
- Using a switch statement instead of a pair map — 3x more code with no benefit.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- What if the input includes other characters like letters and digits? (Ignore them or fail on them?)
- Return the index of the first mismatch instead of just a boolean.
- Extend to validate nested SOQL clauses with parentheses inside WHERE.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why use a Map instead of three separate if/else branches?
The Map gives O(1) dispatch and scales if you add more bracket types (e.g., angle brackets for XML). It's also less code, which Salesforce values.
Could I count opens and closes instead?
Only if there's one bracket type. With three types, counting can't catch '([)]' which is invalid but has matching counts.
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →