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 C code that checks if the parentheses in the string are balanced?
DSA C
#include <stdio.h> #include <string.h> #define MAX 100 int isBalanced(char *expr) { char stack[MAX]; int top = -1; for (int i = 0; i < strlen(expr); i++) { char ch = expr[i]; if (ch == '(') { stack[++top] = ch; } else if (ch == ')') { if (top == -1) return 0; top--; } } return top == -1; } int main() { char expr[] = "((())())"; if (isBalanced(expr)) printf("Balanced\n"); else printf("Not Balanced\n"); return 0; }
Attempts:
2 left
💡 Hint
Check how the stack is used to track opening parentheses and matched with closing ones.
✗ Incorrect
The code uses a stack to push '(' characters and pop when ')' is found. If the stack is empty at the end, parentheses are balanced.
❓ Predict Output
intermediate2:00remaining
Output for Unbalanced Parentheses
What will the following C program print when checking the string "(()" for balanced parentheses?
DSA C
#include <stdio.h> #include <string.h> #define MAX 100 int isBalanced(char *expr) { char stack[MAX]; int top = -1; for (int i = 0; i < strlen(expr); i++) { char ch = expr[i]; if (ch == '(') { stack[++top] = ch; } else if (ch == ')') { if (top == -1) return 0; top--; } } return top == -1; } int main() { char expr[] = "(()"; if (isBalanced(expr)) printf("Balanced\n"); else printf("Not Balanced\n"); return 0; }
Attempts:
2 left
💡 Hint
Count how many '(' are left unmatched at the end.
✗ Incorrect
The stack will have one '(' left unmatched, so the function returns 0 and prints "Not Balanced".
🧠 Conceptual
advanced2:00remaining
Why Use a Stack for 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 open and close in nested order.
✗ Incorrect
A stack follows Last-In-First-Out (LIFO), which matches how the most recent '(' must be closed first by a ')'.
🔧 Debug
advanced2:00remaining
Find the Bug in Parentheses Checker
What error will the following code produce when checking the string ")(" for balanced parentheses?
DSA C
#include <stdio.h> #include <string.h> #define MAX 100 int isBalanced(char *expr) { char stack[MAX]; int top = -1; for (int i = 0; i < strlen(expr); i++) { char ch = expr[i]; if (ch == '(') { stack[++top] = ch; } else if (ch == ')') { top--; } } return top == -1; } int main() { char expr[] = ")("; if (isBalanced(expr)) printf("Balanced\n"); else printf("Not Balanced\n"); return 0; }
Attempts:
2 left
💡 Hint
Look at what happens when ')' is found but stack is empty.
✗ Incorrect
The code decrements top without checking if the stack is empty, causing top to become -2 and invalid memory access.
🚀 Application
expert2:00remaining
Count Balanced Parentheses Substrings
Given the string "()()()", how many balanced parentheses substrings of length 2 are there?
Attempts:
2 left
💡 Hint
Look for every pair of characters that form "()".
✗ Incorrect
Substrings of length 2 are: "()", "()", "()" at positions 0-1, 2-3, 4-5 respectively. All are balanced.
