Workspace Roman To Integer
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Roman to Integer visualizer

Learn Roman to Integer with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice fundamentals and parsing.

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 / 8Execution step
Reference example · JavaScript
Inspect reference input
"MCMXCIV"

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let m={
I:1,V:5,X:10,L:50,C:100,D:500,M:1000
},o=0;
for(let i=0;
i<a.length;
i++){
let x=m[a[i]];
o+=x<(m[a[i+1]]||0)?-x:x;
s("Apply subtractive rule",{
Array:[...a],Total:[o]
},[i])
}
return o
};

Execution canvas

Active Changed
STEP 1

Apply subtractive rule

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

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

Worked example and algorithm walkthrough

Topic: Strings · Fundamentals and parsing

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

"MCMXCIV"

Execution steps

  1. Apply subtractive rule

Computed output

1994
JavaScript reference implementation
const solve = (a,s)=>{
  let m={
    I:1,V:5,X:10,L:50,C:100,D:500,M:1000
  },o=0;
  for(let i=0;
  i<a.length;
  i++){
    let x=m[a[i]];
    o+=x<(m[a[i+1]]||0)?-x:x;
    s("Apply subtractive rule",{
      Array:[...a],Total:[o]
    },[i])
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.