Bird
0
0
DSA Cprogramming~20 mins

Evaluate Postfix Expression Using Stack in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postfix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Postfix Evaluation with Simple Expression
What is the output of the following C code that evaluates a postfix expression using a stack?
DSA C
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX 100

int stack[MAX];
int top = -1;

void push(int val) {
    stack[++top] = val;
}

int pop() {
    return stack[top--];
}

int evaluatePostfix(char* expr) {
    int i;
    for (i = 0; expr[i] != '\0'; i++) {
        if (isdigit(expr[i])) {
            push(expr[i] - '0');
        } else {
            int val2 = pop();
            int val1 = pop();
            switch (expr[i]) {
                case '+': push(val1 + val2); break;
                case '-': push(val1 - val2); break;
                case '*': push(val1 * val2); break;
                case '/': push(val1 / val2); break;
            }
        }
    }
    return pop();
}

int main() {
    char expr[] = "231*+9-";
    printf("%d", evaluatePostfix(expr));
    return 0;
}
A4
B−2
C−4
D−3
Attempts:
2 left
💡 Hint
Trace the postfix expression step-by-step using a stack.
Predict Output
intermediate
2:00remaining
Result of Postfix Expression with Division and Multiplication
What is the output of this C program evaluating the postfix expression "82/3*4+"?
DSA C
#include <stdio.h>
#include <ctype.h>

#define MAX 100

int stack[MAX];
int top = -1;

void push(int val) {
    stack[++top] = val;
}

int pop() {
    return stack[top--];
}

int evaluatePostfix(char* expr) {
    int i;
    for (i = 0; expr[i] != '\0'; i++) {
        if (isdigit(expr[i])) {
            push(expr[i] - '0');
        } else {
            int val2 = pop();
            int val1 = pop();
            switch (expr[i]) {
                case '+': push(val1 + val2); break;
                case '-': push(val1 - val2); break;
                case '*': push(val1 * val2); break;
                case '/': push(val1 / val2); break;
            }
        }
    }
    return pop();
}

int main() {
    char expr[] = "82/3*4+";
    printf("%d", evaluatePostfix(expr));
    return 0;
}
A7
B9
C8
D10
Attempts:
2 left
💡 Hint
Remember integer division truncates towards zero in C.
🔧 Debug
advanced
2:00remaining
Identify the Runtime Error in Postfix Evaluation Code
What runtime error will occur when running this C code with input "34+5*"?
DSA C
#include <stdio.h>
#include <ctype.h>

#define MAX 2

int stack[MAX];
int top = -1;

void push(int val) {
    stack[++top] = val;
}

int pop() {
    return stack[top--];
}

int evaluatePostfix(char* expr) {
    int i;
    for (i = 0; expr[i] != '\0'; i++) {
        if (isdigit(expr[i])) {
            push(expr[i] - '0');
        } else {
            int val2 = pop();
            int val1 = pop();
            switch (expr[i]) {
                case '+': push(val1 + val2); break;
                case '-': push(val1 - val2); break;
                case '*': push(val1 * val2); break;
                case '/': push(val1 / val2); break;
            }
        }
    }
    return pop();
}

int main() {
    char expr[] = "34+5*";
    printf("%d", evaluatePostfix(expr));
    return 0;
}
AStack underflow (reading from empty stack)
BNo runtime error, outputs 35
CStack overflow (writing beyond array bounds)
DDivision by zero error
Attempts:
2 left
💡 Hint
Check the stack size and number of pushes vs pops.
🧠 Conceptual
advanced
1:30remaining
Understanding Stack Operations in Postfix Evaluation
In postfix expression evaluation using a stack, what is the correct order of operations when an operator is encountered?
APop the second operand, then pop the first operand, apply operator as first operand operator second operand
BPop the first operand, then pop the second operand, apply operator as second operand operator first operand
CPop any operand first, order does not matter, apply operator
DPush operands back before applying operator
Attempts:
2 left
💡 Hint
Think about the order of operands in subtraction and division.
🚀 Application
expert
3:00remaining
Final Stack State After Evaluating Complex Postfix Expression
Given the postfix expression "56+72-*3/", what is the state of the stack after evaluating the entire expression using a stack-based postfix evaluator?
DSA C
#include <stdio.h>
#include <ctype.h>

#define MAX 100

int stack[MAX];
int top = -1;

void push(int val) {
    stack[++top] = val;
}

int pop() {
    return stack[top--];
}

void evaluatePostfix(char* expr) {
    int i;
    for (i = 0; expr[i] != '\0'; i++) {
        if (isdigit(expr[i])) {
            push(expr[i] - '0');
        } else {
            int val2 = pop();
            int val1 = pop();
            switch (expr[i]) {
                case '+': push(val1 + val2); break;
                case '-': push(val1 - val2); break;
                case '*': push(val1 * val2); break;
                case '/': push(val1 / val2); break;
            }
        }
    }
}

int main() {
    char expr[] = "56+72-*3/";
    evaluatePostfix(expr);
    printf("Stack top value: %d\n", stack[top]);
    return 0;
}
A[2]
B[1]
C[3]
D[4]
Attempts:
2 left
💡 Hint
Evaluate step-by-step and track the stack after each operator.