GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 13(b)(ii)

Trace second postfix prefix

Data Structures · Stacks & Queues · Full GATE CSE 2000 paper

GATE 2000, Q13(b)(ii) Descriptive
Report issue

Stacks & Queues

Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Tap to enlarge
Answer & solution

Answer: [10, 7, 5, 2], top = 2 (no official key was published)

Step through it (9 steps)

The prefix 5 2 * 3 4 + leaves [10,7]: 5*2=10 and 3+4=7 are two independent partial results. The next token 5 is an operand, so push it to obtain [10,7,5]. Push the next operand 2 to obtain [10,7,5,2]. Stop here: the following multiplication has not yet been read, so 5 and 2 must remain separate entries. The top is 2 and the bottom is 10.

Original paper, page 8