Skip to content

ORIGINAL CODING QUESTION

Evaluate postfix instructions

Evaluate a valid postfix expression. Operands are integers -10..10; tokens 101,102,103 mean addition, subtraction and multiplication. Subtraction uses left operand minus right operand.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, followed by n postfix tokens. Print the final integer.

Constraints

1 ≤ n ≤ 15; expression is valid; intermediate/final results fit signed 32-bit integers.

Example 1

Input

5 2 3 101 4 103

Output

20

(2+3)*4 is 20.

Example 2

Input

3 2 5 102

Output

-3

2-5 is -3.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Operands wait on a stack until an operator arrives.

Hint 2

Pop the right operand before the left operand.

Hint 3

Evaluate left op right and push the result.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →