Challenge - 5 Problems
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Balanced Parentheses Check
What is the output of the following code when checking the string "{[()]}" for balanced parentheses?
DSA Python
def is_balanced(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for char in s: if char in '([{' : stack.append(char) elif char in ')]}' : if not stack or stack.pop() != pairs[char]: return False return not stack print(is_balanced("{[()]}") )
Attempts:
2 left
💡 Hint
Think about how the stack is used to match opening and closing brackets.
✗ Incorrect
The code uses a stack to push opening brackets and pops when a matching closing bracket is found. The string "{[()]}" is perfectly balanced, so the function returns True.
❓ Predict Output
intermediate2:00remaining
Output for Unbalanced Parentheses
What will the function return when checking the string "{[(])}" for balanced parentheses?
DSA Python
def is_balanced(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for char in s: if char in '([{' : stack.append(char) elif char in ')]}' : if not stack or stack.pop() != pairs[char]: return False return not stack print(is_balanced("{[(])}") )
Attempts:
2 left
💡 Hint
Check if the order of brackets is correct in the string.
✗ Incorrect
The string "{[(])}" has mismatched brackets: the closing bracket ")" does not match the last opening bracket "[". So the function returns False.
🔧 Debug
advanced2:00remaining
Identify the Error in Parentheses Checking Code
What error will this code produce when run with input ")()[]{}"?
DSA Python
def check_balanced(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for char in s: if char in '([{' : stack.append(char) elif char in ')]}' : if stack.pop() != pairs[char]: return False return not stack print(check_balanced(")()[]{}"))
Attempts:
2 left
💡 Hint
What happens if you pop from an empty stack?
✗ Incorrect
The code does not check if the stack is empty before popping. When a closing bracket appears but the stack is empty, pop() causes an IndexError.
❓ Predict Output
advanced2:00remaining
Output of Balanced Parentheses with Extra Characters
What is the output of the code when checking the string "a+(b*c)-{d/e}" for balanced parentheses?
DSA Python
def is_balanced(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for char in s: if char in '([{' : stack.append(char) elif char in ')]}' : if not stack or stack.pop() != pairs[char]: return False return not stack print(is_balanced("a+(b*c)-{d/e}"))
Attempts:
2 left
💡 Hint
Ignore characters that are not brackets.
✗ Incorrect
The function ignores non-bracket characters and correctly matches all brackets in the string, so it returns True.
🧠 Conceptual
expert2:00remaining
Stack Usage in Balanced Parentheses Algorithm
Why is a stack the best data structure to check for balanced parentheses in a string?
Attempts:
2 left
💡 Hint
Think about the order in which brackets must be matched.
✗ Incorrect
A stack follows Last-In-First-Out order, which matches the requirement to pair the most recent opening bracket with the next closing bracket.