Bird
0
0
DSA Cprogramming~20 mins

Balanced Parentheses Problem Using Stack in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
ANot Balanced
BRuntime Error
CCompilation Error
DBalanced
Attempts:
2 left
💡 Hint
Check how the stack is used to track opening parentheses and matched with closing ones.
Predict Output
intermediate
2: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;
}
ACompilation Error
BBalanced
CNot Balanced
DRuntime Error
Attempts:
2 left
💡 Hint
Count how many '(' are left unmatched at the end.
🧠 Conceptual
advanced
2:00remaining
Why Use a Stack for Balanced Parentheses?
Why is a stack the best data structure to check for balanced parentheses in an expression?
ABecause it allows checking pairs in the correct nested order using Last-In-First-Out behavior
BBecause it stores all characters and sorts them alphabetically
CBecause it uses First-In-First-Out behavior like a queue
DBecause it can store unlimited characters without memory limits
Attempts:
2 left
💡 Hint
Think about how parentheses open and close in nested order.
🔧 Debug
advanced
2: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;
}
AThe code will cause a compilation error
BThe code will cause a stack underflow (access invalid index)
CThe code will print "Balanced"
DThe code will print "Not Balanced"
Attempts:
2 left
💡 Hint
Look at what happens when ')' is found but stack is empty.
🚀 Application
expert
2:00remaining
Count Balanced Parentheses Substrings
Given the string "()()()", how many balanced parentheses substrings of length 2 are there?
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Look for every pair of characters that form "()".