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 Java code that checks if the parentheses in the string are balanced?
DSA Java
import java.util.Stack; public class BalancedParentheses { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(') { stack.push(c); } else if (c == ')') { if (stack.isEmpty()) { return false; } stack.pop(); } } return stack.isEmpty(); } public static void main(String[] args) { System.out.println(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 finds ')'. If the stack is empty at the end, parentheses are balanced.
❓ Predict Output
intermediateOutput for Unbalanced Parentheses
What will be the output of the following code snippet when checking the string "(()" for balanced parentheses?
DSA Java
import java.util.Stack; public class BalancedParentheses { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(') { stack.push(c); } else if (c == ')') { if (stack.isEmpty()) { return false; } stack.pop(); } } return stack.isEmpty(); } public static void main(String[] args) { System.out.println(isBalanced("(()")); } }
Attempts:
2 left
💡 Hint
Check if all opening parentheses have matching closing ones.
✗ Incorrect
The string "(()" has one '(' without a matching ')', so the stack is not empty at the end.
🔧 Debug
advancedIdentify the Error in Parentheses Checking Code
What error will the following code produce when run with input ")("?
DSA Java
import java.util.Stack; public class BalancedParentheses { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(') { stack.push(c); } else if (c == ')') { if (stack.isEmpty()) { return false; } stack.pop(); } } return stack.isEmpty(); } public static void main(String[] args) { System.out.println(isBalanced(")(")); } }
Attempts:
2 left
💡 Hint
Consider what happens when popping from an empty stack.
✗ Incorrect
The original code pops without checking if the stack is empty, causing an EmptyStackException when input starts with ')'. The fixed code checks if the stack is empty before popping, returning false if it is.
🧠 Conceptual
advancedWhy Use Stack for Balanced Parentheses?
Why is a stack the best data structure to check for balanced parentheses?
Attempts:
2 left
💡 Hint
Think about how parentheses open and close in nested order.
✗ Incorrect
Stack follows last-in, first-out order which matches how parentheses must close in reverse order of opening.
🚀 Application
expertOutput of Balanced Parentheses with Multiple Types
What is the output of the following Java code that checks if the string has balanced parentheses, brackets, and braces?
DSA Java
import java.util.Stack; public class BalancedSymbols { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '[' || c == '{') { stack.push(c); } else if (c == ')' || c == ']' || c == '}') { if (stack.isEmpty()) { return false; } char top = stack.pop(); if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) { return false; } } } return stack.isEmpty(); } public static void main(String[] args) { System.out.println(isBalanced("{[()()]()}")); } }
Attempts:
2 left
💡 Hint
Check matching pairs for all types of brackets.
✗ Incorrect
The code correctly matches all types of brackets and the input string is balanced.
