Evaluate Postfix Expression Using Stack in DSA C - Time & Space Complexity
We analyze how the time needed to evaluate a postfix expression changes as the expression gets longer.
We want to know how the number of steps grows with the size of the expression.
Analyze the time complexity of the following code snippet.
int evaluatePostfix(char* expr) {
Stack s;
initStack(&s); // Initialize the stack before use
for (int i = 0; expr[i] != '\0'; i++) {
if (isdigit(expr[i])) {
push(&s, expr[i] - '0');
} else {
int val2 = pop(&s);
int val1 = pop(&s);
push(&s, applyOp(val1, val2, expr[i]));
}
}
return pop(&s);
}
This code evaluates a postfix expression by scanning each character once and using a stack to compute the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single loop scanning each character in the expression once.
- How many times: Exactly once per character in the input expression.
Each character is processed once, so the total steps grow directly with expression length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows linearly as the expression gets longer.
Time Complexity: O(n)
This means the time to evaluate grows in direct proportion to the length of the postfix expression.
[X] Wrong: "Because of the stack operations, the time complexity is more than linear, maybe quadratic."
[OK] Correct: Each character is pushed or popped at most once, so stack operations add only constant work per character, keeping the total time linear.
Understanding this linear time complexity shows you can analyze how simple data structures like stacks help solve problems efficiently, a key skill in interviews.
"What if the expression included multi-digit numbers separated by spaces? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
Stack -> Option AQuick Check:
Postfix evaluation uses stack = Stack [OK]
- Confusing stack with queue
- Using array without stack operations
- Thinking linked list is primary for postfix
Solution
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).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.Final Answer:
push(s, x); -> Option DQuick Check:
push(stack, value) = push(s, x) [OK]
- Using object.method() syntax in C
- Swapping argument order
- Using undefined function names
"6 2 3 + -" using a stack?Solution
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.Step 2: Verify final stack value
After all operations, the stack has one value: 1.Final Answer:
1 -> Option BQuick Check:
6 - (2 + 3) = 1 [OK]
- Subtracting in wrong order (operand2 - operand1)
- Adding instead of subtracting
- Not pushing intermediate results
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++;
}Solution
Step 1: Check what is pushed onto stack
The code pushes expr[i], which is a character like '5', not the integer 5.Step 2: Explain why this is wrong
Characters have ASCII codes; pushing '5' pushes 53, not integer 5, causing wrong calculations.Final Answer:
Pushing character digit instead of integer value -> Option AQuick Check:
Push integer, not char digit [OK]
- Pushing char instead of int
- Ignoring spaces in expression
- Not handling operators properly
"4 5 7 2 + - *", what is the final evaluated result using stack evaluation?Solution
Step 1: Evaluate inner expression 7 2 +
Push 4, 5, 7, 2. '+' operator: pop 2 and 7, add 7+2=9, push 9.Step 2: Evaluate next operator '-'
Next operator '-', pop 9 and 5, subtract 5-9 = -4, push -4.Step 3: Evaluate final operator '*'
Pop -4 and 4, multiply 4 * -4 = -16, push -16.Final Answer:
-16 -> Option CQuick Check:
4 * (5 - (7 + 2)) = -16 [OK]
- Wrong operator order in subtraction
- Not evaluating nested operations correctly
- Mixing up operand order in multiplication
Ready to prove it?
Pass the test and this pattern moves unknown
