Bird
0
0
DSA Cprogramming~10 mins

Balanced Parentheses Problem Using Stack in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push an element onto the stack.

DSA C
void push(char stack[], int *top, char value) {
    stack[++(*top)] = [1];
}
Drag options to blanks, or click blank then click option'
Astack
B*top
Cvalue
Dtop
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*top' instead of 'value' to push.
Using 'stack' or 'top' incorrectly.
2fill in blank
medium

Complete the code to check if the stack is empty.

DSA C
int isEmpty(int top) {
    return [1] == -1;
}
Drag options to blanks, or click blank then click option'
Astack
Btop
Cvalue
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'stack' or 'value' instead of 'top'.
Using 'size' which is unrelated here.
3fill in blank
hard

Fix the error in the pop function to return the top element and update the top index.

DSA C
char pop(char stack[], int *top) {
    if (*top == -1) return '\0';
    return stack[[1]--];
}
Drag options to blanks, or click blank then click option'
A(*top)
Btop
Cstack
D*top
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' instead of '*top' causes wrong indexing.
Using 'stack' or '(*top)' incorrectly.
4fill in blank
hard

Fill both blanks to check if the popped opening bracket matches the current closing bracket.

DSA C
int isMatchingPair(char opening, char closing) {
    return (opening == [1] && closing == [2]) ||
           (opening == '{' && closing == '}') ||
           (opening == '[' && closing == ']');
}
Drag options to blanks, or click blank then click option'
A'('
B'['
C')'
D']'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing opening and closing brackets.
Using double quotes instead of single quotes.
5fill in blank
hard

Fill all three blanks to complete the main loop that checks balanced parentheses.

DSA C
for (int i = 0; i < length; i++) {
    if (expression[i] == [1] || expression[i] == [2] || expression[i] == [3]) {
        push(stack, &top, expression[i]);
    } else {
        if (isEmpty(top)) return 0;
        char popped = pop(stack, &top);
        if (!isMatchingPair(popped, expression[i])) return 0;
    }
}
Drag options to blanks, or click blank then click option'
A'('
B'['
C'{'
D')'
Attempts:
3 left
💡 Hint
Common Mistakes
Including closing brackets in the push condition.
Using wrong bracket characters.