What is the maximum number of elements present simultaneously in the operand…

2023

What is the maximum number of elements present simultaneously in the operand stack while evaluating the postfix expression:

6 2 3 + - 3 8 2 / + *

  1. A.

    1

  2. B.

    2

  3. C.

    3

  4. D.

    4

  5. E.

    None of the above

Attempted by 30 students.

Show answer & explanation

Correct answer: D

Concept

Postfix (Reverse Polish) evaluation uses one stack. Scan tokens left to right: PUSH every operand; on an operator POP exactly two operands, compute the result, and PUSH that single result back. Each binary operator therefore replaces two stack entries with one, so it lowers the stack height by 1, while each operand raises it by 1. The "maximum number of elements in the stack" is simply the largest height the stack ever reaches during this scan, not the final stack size.

Application — trace 6 2 3 + - 3 8 2 / + *

Process one token at a time and record the stack height after each step (height = number of values currently on the stack):

  1. 6 -> push 6; stack [6]; height 1

  2. 2 -> push 2; stack [6, 2]; height 2

  3. 3 -> push 3; stack [6, 2, 3]; height 3

  4. + -> pop 3 and 2, push 2+3=5; stack [6, 5]; height 2

  5. - -> pop 5 and 6, push 6-5=1; stack [1]; height 1

  6. 3 -> push 3; stack [1, 3]; height 2

  7. 8 -> push 8; stack [1, 3, 8]; height 3

  8. 2 -> push 2; stack [1, 3, 8, 2]; height 4

  9. / -> pop 2 and 8, push 8/2=4; stack [1, 3, 4]; height 3

  10. + -> pop 4 and 3, push 3+4=7; stack [1, 7]; height 2

  11. * -> pop 7 and 1, push 1*7=7; stack [7]; height 1

The heights reached are 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1. The peak height is 4, reached just after pushing the operand 2 in the run 3 8 2 (stack [1, 3, 8, 2]), immediately before the / operator collapses the top two values.

Cross-check

Quick sanity check: the longest unbroken run of operands before any operator is the segment 1 3 8 2 (the partial result 1, then three fresh operands 3, 8, 2), giving four values on the stack at once. No later point stacks more, and the expression evaluates to 7, confirming a well-formed postfix string. Hence the maximum number of elements simultaneously on the stack is 4.

Explore the full course: Niacl Ao It Specialist

Loading lesson…