Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main data structure used to evaluate a postfix expression?
easy
A. Stack
B. Queue
C. Array
D. Linked List

Solution

  1. Step 1: Understand postfix evaluation process

    Postfix expressions are evaluated by scanning from left to right and using a data structure to store operands temporarily.
  2. Step 2: Identify the suitable data structure

    A stack is used because it allows pushing operands and popping them when an operator is encountered, following Last-In-First-Out order.
  3. Final Answer:

    Stack -> Option A
  4. Quick Check:

    Postfix evaluation uses stack = Stack [OK]
Hint: Remember: postfix needs LIFO, so use stack [OK]
Common Mistakes:
  • Confusing stack with queue
  • Using array without stack operations
  • Thinking linked list is primary for postfix
2. Which of the following is the correct syntax to push an integer value 'x' onto a stack named 's' in C?
easy
A. stack_push(x);
B. s.push(x);
C. push(x, s);
D. push(s, x);

Solution

  1. Step 1: Understand typical stack push function signature

    In C, stack push usually takes the stack and the value to push as arguments, e.g., push(stack, value).
  2. Step 2: Match the correct syntax

    push(s, x); matches the common pattern push(s, x); where 's' is the stack and 'x' is the value.
  3. Final Answer:

    push(s, x); -> Option D
  4. Quick Check:

    push(stack, value) = push(s, x) [OK]
Hint: Remember: push(stack, value) in C [OK]
Common Mistakes:
  • Using object.method() syntax in C
  • Swapping argument order
  • Using undefined function names
3. What is the result of evaluating the postfix expression "6 2 3 + -" using a stack?
medium
A. 3
B. 1
C. 5
D. -1

Solution

  1. Step 1: Evaluate postfix step-by-step

    Push 6, push 2, push 3. Encounter '+', pop 3 and 2, add: 2+3=5, push 5. Encounter '-', pop 5 and 6, subtract: 6-5=1.
  2. Step 2: Verify final stack value

    After all operations, the stack has one value: 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    6 - (2 + 3) = 1 [OK]
Hint: Process operators by popping two operands [OK]
Common Mistakes:
  • Subtracting in wrong order (operand2 - operand1)
  • Adding instead of subtracting
  • Not pushing intermediate results
4. Identify the error in this C code snippet for postfix evaluation:
char* expr = "5 9 8 + 4 6 * * 7 + *";
int i = 0;
while(expr[i] != '\0') {
  if(isdigit(expr[i])) push(stack, expr[i]);
  else { /* operator handling */ }
  i++;
}
medium
A. Pushing character digit instead of integer value
B. Using while loop instead of for loop
C. Missing stack initialization
D. Incorrect operator handling

Solution

  1. Step 1: Check what is pushed onto stack

    The code pushes expr[i], which is a character like '5', not the integer 5.
  2. Step 2: Explain why this is wrong

    Characters have ASCII codes; pushing '5' pushes 53, not integer 5, causing wrong calculations.
  3. Final Answer:

    Pushing character digit instead of integer value -> Option A
  4. Quick Check:

    Push integer, not char digit [OK]
Hint: Convert char to int before push: expr[i] - '0' [OK]
Common Mistakes:
  • Pushing char instead of int
  • Ignoring spaces in expression
  • Not handling operators properly
5. Given the postfix expression "4 5 7 2 + - *", what is the final evaluated result using stack evaluation?
hard
A. -24
B. 16
C. -16
D. 24

Solution

  1. Step 1: Evaluate inner expression 7 2 +

    Push 4, 5, 7, 2. '+' operator: pop 2 and 7, add 7+2=9, push 9.
  2. Step 2: Evaluate next operator '-'

    Next operator '-', pop 9 and 5, subtract 5-9 = -4, push -4.
  3. Step 3: Evaluate final operator '*'

    Pop -4 and 4, multiply 4 * -4 = -16, push -16.
  4. Final Answer:

    -16 -> Option C
  5. Quick Check:

    4 * (5 - (7 + 2)) = -16 [OK]
Hint: Evaluate inner expressions first, then combine [OK]
Common Mistakes:
  • Wrong operator order in subtraction
  • Not evaluating nested operations correctly
  • Mixing up operand order in multiplication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard