20. Valid Parentheses
easyAsked at HubSpotHubSpot asks Valid Parentheses to test stack intuition and edge-case discipline — skills that surface constantly when parsing template syntax, email tokens, or workflow expression strings in their CRM platform.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in HubSpot loops.
- Glassdoor (2026-Q1)— Cited as a frequent HubSpot phone-screen problem in SWE candidate reports.
- r/cscareerquestions (2025-10)— HubSpot candidates mention Valid Parentheses as a common early-round screening problem.
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 = "()[]{}"trueExplanation: Each open bracket is immediately closed by its matching close bracket.
Example 2
s = "([)]"falseExplanation: The brackets are interleaved incorrectly — the inner pair is closed before the outer pair.
Approaches
1. Stack with matching map
Push every open bracket onto a stack. When you see a close bracket, pop the top and verify it matches. If the stack is empty at the end, the string is valid.
- Time
- O(n)
- Space
- O(n)
function isValid(s) {
const stack = [];
const match = { ')': '(', '}': '{', ']': '[' };
for (const ch of s) {
if (ch === '(' || ch === '{' || ch === '[') {
stack.push(ch);
} else {
if (stack.length === 0 || stack[stack.length - 1] !== match[ch]) {
return false;
}
stack.pop();
}
}
return stack.length === 0;
}Tradeoff: Clean O(n) time and O(n) space. The closing-bracket map keeps the conditional logic compact. This is the canonical answer HubSpot expects — mention the empty-stack check before popping to handle strings that start with a close bracket.
HubSpot-specific tips
Interviewers at HubSpot appreciate when you immediately justify using a stack: 'Brackets need to be matched in LIFO order — that's a stack.' Walk through the edge cases before coding: empty string, all opens with no closes, a single character. Relating it to parsing HubSpot workflow expressions or HubL template tags shows domain awareness that resonates with the team.
Common mistakes
- Forgetting to check if the stack is empty before popping — causes a runtime error on strings like ']'.
- Returning true without checking that the stack is empty at the end — '((' would incorrectly pass.
- Using an array of close brackets to check membership instead of a map — leads to verbose if-else chains.
- Not handling the case where the string has an odd length — though the algorithm handles it automatically, stating it upfront impresses interviewers.
Follow-up questions
An interviewer at HubSpot may pivot to one of these next:
- Generate Parentheses (LC 22) — generate all valid combinations of n pairs.
- Longest Valid Parentheses (LC 32) — find the length of the longest valid substring.
- What if the string could also contain letters — how would you skip non-bracket characters?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a stack and not a counter?
A counter works for a single bracket type but fails for mixed types like '([)]'. A stack preserves the ordering context needed to validate nesting across multiple bracket types.
Can this be done in O(1) space?
Not in general — you need to remember which opens are unmatched. For a single bracket type a counter suffices, but for three types you fundamentally need a stack.
Does an empty string return true?
Yes — the stack starts empty and stays empty, so `stack.length === 0` returns true. The problem constraints say length >= 1, but it's worth noting.
Practice these live with InterviewChamp.AI
Drill Valid Parentheses and other HubSpot interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →