Cover image for Amazon
Logo

Amazon

Engaged Employer

Amazon

Add an Interview

Interview Question

Software Development Engineer Interview

-

Amazon

Programming question - Given reverse polish notation, return the integer value of the expression aka "5,3,+,2,/" would be 5 + 3 = 8 and then 8 / 2 = 4.

Interview Answer

1 Answer

14

Use a stack. Push values onto the stack. When the token is an operator, pop the 2 values from the stack and apply the operator, pushing the result back onto the stack. Make sure to put the values on the correct sides of the operator. At the end, if it's a valid postfix notation or "reverse polish notation", there will be a single value on the stack. That's the answer. This notation is neat because there's no ambiguity, no need for parentheses.

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.