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 C++ code that checks if the parentheses in the string are balanced?
DSA C++
#include <iostream> #include <stack> #include <string> bool isBalanced(const std::string& s) { std::stack<char> st; for (char c : s) { if (c == '(') { st.push(c); } else if (c == ')') { if (st.empty()) return false; st.pop(); } } return st.empty(); } int main() { std::string expr = "(()())"; if (isBalanced(expr)) { std::cout << "Balanced" << std::endl; } else { std::cout << "Not Balanced" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Check how the stack is used to match opening and closing parentheses.
✗ Incorrect
The code pushes '(' onto the stack and pops when it finds ')'. The string "(()())" is balanced, so the stack is empty at the end, resulting in "Balanced" output.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Balanced Parentheses
Why is a stack the best data structure to check for balanced parentheses in an expression?
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. This makes it ideal for this problem.
🔧 Debug
advancedIdentify the Error in Parentheses Checking Code
What error will the following code produce when checking the string "(()" for balanced parentheses?
DSA C++
#include <iostream> #include <stack> #include <string> bool isBalanced(const std::string& s) { std::stack<char> st; for (char c : s) { if (c == '(') { st.push(c); } else if (c == ')') { if (st.empty()) return false; st.pop(); } } return true; } int main() { std::string expr = "(()"; if (isBalanced(expr)) { std::cout << "Balanced" << std::endl; } else { std::cout << "Not Balanced" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Check the return statement of the function and what it means for leftover opening parentheses.
✗ Incorrect
The function returns true without checking if the stack is empty at the end, so leftover '(' are ignored, causing incorrect "Balanced" output.
❓ Predict Output
advancedOutput of Balanced Parentheses with Multiple Types
What is the output of this C++ code that checks balanced parentheses, brackets, and braces?
DSA C++
#include <iostream> #include <stack> #include <string> bool isBalanced(const std::string& s) { std::stack<char> st; for (char c : s) { if (c == '(' || c == '{' || c == '[') { st.push(c); } else { if (st.empty()) return false; char top = st.top(); if ((c == ')' && top == '(') || (c == '}' && top == '{') || (c == ']' && top == '[')) { st.pop(); } else { return false; } } } return st.empty(); } int main() { std::string expr = "[{()}]"; std::cout << (isBalanced(expr) ? "Balanced" : "Not Balanced") << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check matching pairs for all three types of brackets.
✗ Incorrect
The string "[{()}]" is correctly nested with matching pairs, so the output is "Balanced".
🧠 Conceptual
expertTime Complexity of Balanced Parentheses Algorithm
What is the time complexity of checking balanced parentheses using a stack for a string of length n?
Attempts:
2 left
💡 Hint
Consider how many times each character is visited and how costly stack operations are.
✗ Incorrect
Each character is processed once, and stack push/pop operations are O(1), so total time is O(n).
