The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
3.9k views

The following postfix expression with single digit operands is evaluated using a stack:

$$8 \ 2 \ 3 \ ^\hat{} /  \ 2 \ 3 * + 5 \ 1 * -$$

Note that $^\hat{}$ is the exponentiation operator. The top two elements of the stack after the first $*$ is evaluated are

  1. $6, 1$
  2. $5, 7$
  3. $3, 2$
  4. $1, 5$
asked in DS by Veteran (52k points)
edited by | 3.9k views
+4
The full question is not visible in GO hardcopy book which is printed.Only 823^ is visible and after some tabs /.

Anyone having GO physical copy please validate my claim.
0
Yeah it is
+1

@Arjun FYI

This question is not properly visible in the GO PDF and the hardcopy as well. The claim for inconsistency in hardcopy has been raised by @sripo and validated by @Yaduvanshi Rishi.

0
This question is not completely written in GO pdf, only half part is visible there.

Please correct it.

3 Answers

+28 votes
Best answer

push $8$  so stack is $8$

push $2$ so stack is $8 \ 2$

push $8 \ 2 \ 3$

$^\hat{}$ pop $3$ and $2$ perform opn $2 ^\hat{} 3$ and push to stack. stack is $8 \ 8$

/ pop $8$ and $8$ perform $8/8$ and push result to stack . stack is $1$

push $2$ stack is $1 \ 2$

push $3$ stack is $1 \ 2 \ 3$

$*$ pop $3$ and $2$ perform by $2*3$ and push . stack is $1 \ 6$

Hence, answer is A.

answered by Boss (11.1k points)
edited by
0

after 823 we've ^ in TOS , so we'll pop last two element & perform the operation that's fine.but my query is that why 2^3 not 3^2 & in case there is ' - ' in place of ' ^ ' then in which order it will be performed.

I mean sometimes for some operator order of the elelment will matter( e.g:' - ' & ' ^ ' as 3-2 or 2-3  and  2^3 or 3^2) Is there any general concept to remove this confusion???

0

@MRINMOY_HALDER......bottom of the element is 1st and top element 2nd......i.e    2-3=-1

better take any infix operation solve first then convert that postfix evaluate using postfix evaluation...you will get ans

+6 votes
Operation   Expression   Stack 
   823 ^ /23 * 51 * -   Empty 
Push  23 ^ /23 * 51 * -  8
Push  3 ^ /23 * 51 * -  8 2
Push  ^ /23 * 51 * -  8 2 3
Pop  /23 * 51 * -  8 (2^3)
Pop  23 * 51 * -  8/8
Push   3 * 51 * -  1 2
Push  * 51 * -  1 2 3
Pop  51 * -  1 (2*3)

So top two elements after 1st * is 6 , 1

answered by Boss (10.5k points)
–3 votes
Ans is (A) 6 ,1
answered by Loyal (7.5k points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,535 questions
54,121 answers
187,313 comments
71,035 users