Skip to content
Algorithms for Beginners

Problems / Stack

Postfix calculator

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

In postfix notation, each operator comes after the two numbers it works on. 3 4 + means 3 + 4. Longer sums need no brackets: 3 4 + 2 * means (3 + 4) × 2 = 14, and 3 4 2 * + means 3 + (4 × 2) = 11.

tokens is a list of numbers and operators, as strings. The operators are +, -, * and /. Return the result.

Division rounds towards zero, dropping any fraction: 7 / 2 is 3, and -7 / 2 is -3. (Math.trunc does exactly this.)

Examples

  1. Example 1

    Input
    tokens = ["3", "4", "+", "2", "*"]
    Output
    14

    3 + 4 = 7, then 7 × 2 = 14.

  2. Example 2

    Input
    tokens = ["3", "4", "2", "*", "+"]
    Output
    11

    4 × 2 = 8, then 3 + 8 = 11.

  3. Example 3

    Input
    tokens = ["9", "2", "-", "3", "/"]
    Output
    2

    9 − 2 = 7, then 7 ÷ 3 = 2.33…, which rounds towards zero to 2.

Rules

  • tokens is always a valid postfix sum, with between 1 and 100,000 tokens.
  • Numbers are whole numbers, and can be negative, like "-3". No division by zero happens.
  • Every intermediate result stays between -1,000,000,000 and 1,000,000,000.
Words on this page, in plain English (2)Open if any word is new to you
String
A piece of text, written between quotes. Each letter in it is a character.
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function postfixCalculator(tokens) {
  // Return the result.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in stack

  • Learn the stack pattern

    Keep the most recent unfinished thing on top, and finish it first.

  • Tidy a file path

    Medium. Turn a messy folder path with dots and extra slashes into its simplest form.

  • Days until warmer

    Medium. For each day, count how many days you'd wait for a warmer one.

  • Balanced brackets

    Easy. Check whether every bracket in a string is closed by the right kind, in the right order.

  • Typing with backspace

    Easy. Work out what's on the screen after someone types, including presses of the backspace key.

  • Stack with minimum

    Medium. Build a stack that can also tell you its smallest item instantly.