Roman to Integer solution explained

To convert a Roman numeral to an integer, add up the symbol values, except that a symbol is subtracted when the symbol right after it is worth more (the I in IV, the X in XC). One pass with a seven-entry lookup table does it, comparing each symbol with its right neighbor, or scanning from the right and subtracting anything smaller than the largest value seen so far. Both run in O(n) time and O(1) memory, and n is at most 15 for numerals up to 3999.

By Published 6 min read

The problem in plain words

Roman numerals use seven letters: I is 1, V is 5, X is 10, L is 50, C is 100, D is 500 and M is 1000. Normally the letters run from largest to smallest and you add them, so MMXXVI is 1000 + 1000 + 10 + 10 + 5 + 1 = 2026. Six combinations break the descending order on purpose: IV and IX mean 4 and 9, XL and XC mean 40 and 90, CD and CM mean 400 and 900. In each one, a smaller letter placed before a larger one is taken away from it. You are given a string that is a correct numeral between 1 and 3999 and must return the number.

Examples computed with the code on this page
NumeralValueHow it breaks down
XIV14X + IV
XLIX49XL + IX, two subtractive pairs back to back
CXL140C + XL
CDXLIV444CD + XL + IV, every place uses a pair
DCCCXC890D + CCC + XC
MMXXVI2026Purely additive
MMMCMXCIX3999The largest value the standard form can write
Examples computed with the code on this page. Scroll sideways on a small screen.

What the question is really checking

This is usually an early warm-up, so the bar is a clean, bug-free answer in a few minutes. The interviewer wants to see you turn the six special cases into one general rule rather than six if-statements, pick a lookup structure instead of a long switch, and say what happens with input outside the promise. Some interviewers follow up with the reverse direction, Integer to Roman, which needs a different idea, so finishing quickly is useful.

Likely follow-ups

  • Reject invalid numerals such as IIII, VX or IC. The simple rule happily returns a number for them. A strict check either matches the string against the standard pattern (thousands, then hundreds, tens and ones, each from a fixed set of forms) or converts back and compares.
  • Lowercase input. Normalize first, or add lowercase keys to the table.
  • Larger numbers. Historical notations for values above 3999 used a bar over letters; there is no standard ASCII form, so ask what the input looks like.
  • Stream the characters. The right-to-left version needs the whole string; the left-to-right version needs only one character of lookahead.

First version: check for a pair, then a single letter

Store the six subtractive pairs with their values and the seven single letters with theirs. At each position, look at the next two characters. If they form one of the six pairs, add the pair's value and jump two places; otherwise add the single letter and move one place. It is direct and readable, and it works because in a valid numeral the pairs never overlap.

@@roman-to-integer/slow.py@@
Python: two-character lookahead with a pair table. O(n) time, O(1) memory.

Nothing is wrong with this answer, and some interviewers prefer it for clarity. Its weakness is that the six pairs are data you had to remember. If you forget CD, the code quietly returns 600 for CD instead of 400.

Expected version: compare with the neighbor

All six pairs follow one rule: the first letter is worth less than the second. So, for each letter, look at the letter after it. If the next one is larger, subtract the current value; otherwise add it. In CDXLIV that gives -100 + 500 - 10 + 50 - 1 + 5 = 444. The pattern is a single pass with one element of lookahead, and the table shrinks to the seven letters everyone knows.

@@roman-to-integer/fast.py@@
Python: subtract when a larger symbol follows. O(n) time, O(1) memory.

The same rule can be read from the right end, which removes the lookahead. Walk backward and remember the largest value seen so far. A letter smaller than that maximum sits in front of something bigger, so it is subtracted; otherwise it is added and becomes the new maximum. For valid input both directions agree on every value from 1 to 3999, which was checked.

@@roman-to-integer/fast.js@@
JavaScript: right to left, subtracting anything smaller than the largest symbol to its right.

Cost

n is the numeral's length; for 1 to 3999 it is at most 15 (MMMDCCCLXXXVIII)
ApproachTimeMemoryNote
Pair table, then single lettersO(n)O(1)Needs all six pairs listed correctly
Compare with the next symbolO(n)O(1)One rule, seven-entry table
Right to left with a running maximumO(n)O(1)No lookahead; same results on valid input
n is the numeral's length; for 1 to 3999 it is at most 15 (MMMDCCCLXXXVIII). Scroll sideways on a small screen.

Edge cases

  • A single letter, such as I or M, where there is no neighbor to compare with.
  • Pairs back to back, as in XLIX and CDXLIV.
  • The last character, which is always added (nothing follows it).
  • The empty string, which a valid input never is; returning 0 is a sensible default if you are asked.
  • Characters outside the seven letters. With the promise of valid input you may ignore them, but say that a dictionary lookup will raise a KeyError in Python and give NaN in JavaScript.

Common slips

  • Comparing with the previous letter instead of the next one, then trying to undo a value that was already added. That can work (add the current value, and subtract twice the previous one if it was smaller), but it is easy to get wrong.
  • Using less-than-or-equal in the comparison, which turns II into 0.
  • Reading past the end of the string when checking the neighbor.
  • Assuming the function validates input. Mention that VX gives 5 and IIII gives 4 under this rule, even though neither is a standard numeral.

Saying it out loud

Fictional example

From six special cases to one rule
Candidate
I see six special pairs, IV, IX, XL, XC, CD and CM. In every one, the first letter is smaller than the second, so I'll treat that as the rule instead of listing them.
Candidate
Walk the string, and for each letter, if the next letter is bigger, subtract this one; otherwise add it. CDXLIV: minus a hundred, plus five hundred, minus ten, plus fifty, minus one, plus five. That's 444.
Candidate
It's one pass with a seven-entry dictionary. I'm trusting that the input is valid; if you want invalid numerals rejected, I'd match against the standard pattern or convert back and compare.

Integer to Roman goes the other way and is solved with a greedy table instead of a neighbor comparison. String to Integer (atoi) is another single left-to-right scan that turns characters into a number, with rules about where to stop. Valid Parentheses is a different warm-up where each character also depends on what comes around it.

Natively for practice

Warm-up questions are where nerves cost the most, because there is little time to recover from a slow start. While you rehearse on Mac or Windows, Natively can pick up the question from a screenshot, check your one-rule version against the pair-table version, and suggest a follow-up such as validation to practice next. Its transcript of the session shows how long you spent before the first line of code.

Questions people ask

How do you convert a Roman numeral to an integer?

Walk the numeral once. For each symbol, subtract its value if the next symbol is worth more, otherwise add it. CDXLIV becomes -100 + 500 - 10 + 50 - 1 + 5 = 444.

What is the time complexity of Roman to Integer?

O(n) time and O(1) memory, where n is the numeral's length. For numerals from 1 to 3999, n is at most 15, so in practice it is constant.

Why scan Roman numerals from right to left?

From the right you only need the largest value seen so far: a smaller symbol is subtracted, anything else is added. It avoids looking ahead and gives the same result on valid input.

Does Roman to Integer need to validate the numeral?

Usually not; the input is promised to be valid. If asked, match it against the standard thousands, hundreds, tens and ones forms, or convert the result back to Roman and compare.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP