Evaluate Postfix Arithmetic with Truncating Integer Division

Quick Overview

Evaluate postfix arithmetic with ordered stack operands and exact integer division truncating toward zero.

Evaluate Postfix Arithmetic with Truncating Integer Division

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Evaluate a valid arithmetic expression written in reverse Polish notation. Tokens are integer operands or the binary operators `+`, `-`, `*`, and `/`. Division truncates toward zero, and no division by zero occurs. Return the resulting integer. ```cpp int evalRPN(vector<string>& tokens); ``` ### Examples ```text Input: tokens = ["8","3","-","2","*"] Output: 10 ``` ```text Input: tokens = ["-7","3","/"] Output: -2 ``` ### Constraints - There are between 1 and 10000 tokens. - Numeric tokens represent integers between -200 and 200. - The expression is valid, and its answer and every intermediate calculation fit signed 32-bit integers.

Overview: Evaluate postfix arithmetic with ordered stack operands and exact integer division truncating toward zero.

Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Evaluate a valid arithmetic expression written in reverse Polish notation. Tokens are integer operands or the binary operators +, -, *, and /. Division truncates toward zero, and no division by zero occurs. Return the resulting integer.

int evalRPN(vector<string>& tokens);

Examples

Input: tokens = ["8","3","-","2","*"]
Output: 10
Input: tokens = ["-7","3","/"]
Output: -2

Constraints

  • There are between 1 and 10000 tokens.
  • Numeric tokens represent integers between -200 and 200.
  • The expression is valid, and its answer and every intermediate calculation fit signed 32-bit integers.