Workspace Evaluate Reverse Polish Notation
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Evaluate Reverse Polish Notation visualizer

Learn Evaluate Reverse Polish Notation with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice operations and expressions.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 6Execution step
Reference example · JavaScript
Inspect reference input
[
  "2",
  "1",
  "+",
  "3",
  "*"
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let st=[];
for(let x of a){
if(["+","-","*","/"].includes(x)){
let b=st.pop(),c=st.pop();
st.push(x==="+"?c+b:x==="-"?c-b:x==="*"?c*b:Math.trunc(c/b))
}else st.push(+x);
s("Process "+x,{
Stack:st
})
}
return st[0]
};

Execution canvas

Active Changed
STEP 1

Process 2

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Stack1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Stacks & Queues · Operations and expressions

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

[
  "2",
  "1",
  "+",
  "3",
  "*"
]

Execution steps

  1. Process 2
  2. Process 1
  3. Process +
  4. Process 3
  5. Process *

Computed output

9
JavaScript reference implementation
const solve = (a,s)=>{
  let st=[];
  for(let x of a){
    if(["+","-","*","/"].includes(x)){
      let b=st.pop(),c=st.pop();
      st.push(x==="+"?c+b:x==="-"?c-b:x==="*"?c*b:Math.trunc(c/b))
    }else st.push(+x);
    s("Process "+x,{
      Stack:st
    })
  }
  return st[0]
};

Continue learning

Practice related DSA questions in the same topic.