Challenge - 5 Problems
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code checking balanced parentheses?
Consider the following JavaScript code that checks if parentheses in a string are balanced using a stack. What will be printed after running it?
DSA Javascript
function isBalanced(s) {
const stack = [];
for (const char of s) {
if (char === '(') {
stack.push(char);
} else if (char === ')') {
if (stack.length === 0) return false;
stack.pop();
}
}
return stack.length === 0;
}
console.log(isBalanced('(()())'));Attempts:
2 left
💡 Hint
Think about how the stack is used to match opening and closing parentheses.
✗ Incorrect
The code pushes '(' onto the stack and pops when it sees ')'. The string '(()())' is balanced, so the stack is empty at the end, returning true.
❓ Predict Output
intermediateWhat is the output when input has extra closing parentheses?
What will the following code print when checking the string '(()))' for balanced parentheses?
DSA Javascript
function isBalanced(s) {
const stack = [];
for (const char of s) {
if (char === '(') {
stack.push(char);
} else if (char === ')') {
if (stack.length === 0) return false;
stack.pop();
}
}
return stack.length === 0;
}
console.log(isBalanced('(()))'));Attempts:
2 left
💡 Hint
Check what happens when a closing parenthesis appears but stack is empty.
✗ Incorrect
The string has one extra closing parenthesis, so the function returns false.
🧠 Conceptual
advancedWhich data structure is best for checking balanced parentheses and why?
Why is a stack the best data structure to check if parentheses in a string are balanced?
Attempts:
2 left
💡 Hint
Think about how parentheses must be matched in reverse order of appearance.
✗ Incorrect
A stack works as last-in, first-out, so the most recent '(' is matched with the next ')'. This matches the nesting structure of parentheses.
❓ Predict Output
advancedWhat is the output of this code with multiple types of brackets?
What will the following code print when checking the string '{[()()]}' for balanced brackets of types (), {}, and []?
DSA Javascript
function isBalanced(s) {
const stack = [];
const pairs = {')': '(', ']': '[', '}': '{'};
for (const char of s) {
if (['(', '[', '{'].includes(char)) {
stack.push(char);
} else if ([')', ']', '}'].includes(char)) {
if (stack.length === 0 || stack.pop() !== pairs[char]) return false;
}
}
return stack.length === 0;
}
console.log(isBalanced('{[()()]}'));Attempts:
2 left
💡 Hint
Check how the code matches each closing bracket with the correct opening bracket.
✗ Incorrect
All brackets are correctly matched and nested, so the function returns true.
🔧 Debug
expertWhat error does this code raise when checking '(()'?
What error will this code raise when checking the string '(()' for balanced parentheses?
DSA Javascript
function isBalanced(s) {
const stack = [];
for (const char of s) {
if (char === '(') {
stack.push(char);
} else if (char === ')') {
stack.pop();
}
}
return stack.length === 0;
}
console.log(isBalanced('(()'));Attempts:
2 left
💡 Hint
Check what happens if there are unmatched opening parentheses left in the stack.
✗ Incorrect
The code does not check if stack is empty before popping, but since input has extra '(', no pop error occurs. The function returns false because stack is not empty.
