Challenge - 5 Problems
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Balanced Parentheses Check
What is the output of the following TypeScript code that checks if the parentheses in the string are balanced?
DSA Typescript
function isBalanced(s: string): boolean {
const stack: string[] = [];
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 track opening parentheses and matched with closing ones.
ā Incorrect
The function pushes '(' onto the stack and pops when it finds ')'. The string '(()())' is balanced, so the stack ends empty and returns true.
ā Predict Output
intermediateOutput for Unbalanced Parentheses
What will the following TypeScript code print when checking the string '(())())' for balanced parentheses?
DSA Typescript
function isBalanced(s: string): boolean {
const stack: string[] = [];
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
Count the number of opening and closing parentheses carefully.
ā Incorrect
The string has one extra closing parenthesis without a matching opening one, so the function returns false.
š§ Debug
advancedIdentify the Error in Parentheses Checking Code
What error will this TypeScript code produce when checking balanced parentheses?
DSA Typescript
function isBalanced(s: string): boolean {
const stack: string[] = [];
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
Look carefully at how the pop method is called.
ā Incorrect
The original code used stack.pop without parentheses, so the pop method was not called. The fixed code calls stack.pop() correctly, so it returns the correct boolean result.
š§ Conceptual
advancedStack Usage in Balanced Parentheses Problem
Why is a stack the best data structure to check for balanced parentheses in a string?
Attempts:
2 left
š” Hint
Think about how parentheses must close in the reverse order they open.
ā Incorrect
A stack follows last-in, first-out order, which matches how parentheses must be closed in reverse order of opening.
š Application
expertOutput of Balanced Parentheses with Multiple Types
What is the output of this TypeScript code that checks balanced parentheses including (), {}, and []?
DSA Typescript
function isBalanced(s: string): boolean {
const stack: string[] = [];
const pairs: {[key: string]: string} = {')': '(', '}': '{', ']': '['};
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 if all types of brackets are matched correctly in order.
ā Incorrect
The string '{[()]}' is balanced with matching pairs in correct order, so the function returns true.
