EasyHash TableMathStringUpdated Sep 2026

13. Roman to Integer

Asked at Google, Microsoft, Oracle, Uber and 1 more

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Hash Table

Companies

5

Problem

You are given a string s that holds a valid Roman numeral. Convert it to the integer it represents and return that integer.

Roman numerals use seven letters, each with a fixed value: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Most of the time you just add the letters together, reading from left to right, and the values go from big to small. For example, XII is 10 + 1 + 1 = 12.

The tricky part is subtractive notation. When a smaller value sits directly before a larger one, you subtract it instead of adding it. So IV is 5 − 1 = 4, not 6, and IX is 9. Only six such pairs exist: IV, IX, XL, XC, CD, CM. The input is guaranteed to be a valid numeral in the range 1 to 3999, so you do not need to check for bad input.

Add normally, but subtract when a smaller letter comes first

sMCMXCIV0123456CM = 900, XC = 90, IV = 4, and the lone M = 1000.
In "MCMXCIV" the pairs CM, XC and IV are subtractive, so each one is worth the larger value minus the smaller.

Constraints

  • ●1 <= s.length <= 15
  • ●s contains only the characters 'I', 'V', 'X', 'L', 'C', 'D', 'M'
  • ●s is a valid Roman numeral in the range [1, 3999]

Examples

Example 1
Inputs = "III"
Output3
WhyThree `I` letters, each worth 1, and no letter is smaller than the one after it. So 1 + 1 + 1 = 3.
Example 2
Inputs = "LVIII"
Output58
Why`L` is 50, `V` is 5, and the three `I` letters add 3. Everything is additive here: 50 + 5 + 1 + 1 + 1 = 58.
Example 3
Inputs = "MCMXCIV"
Output1994
WhyBreak it into pairs: `M`=1000, `CM`=900, `XC`=90, `IV`=4. Adding them gives 1000 + 900 + 90 + 4 = 1994.

Asked At

5 companies

How to Think About It

1

Key insight: Roman numerals are additive from left to right, EXCEPT when a smaller value appears before a larger value (subtractive). For example, IV = 5 - 1 = 4, but VI = 5 + 1 = 6. Scan from right to left: if the current value is less than the previous value, subtract it; otherwise, add it.

2

The hash map approach: map each Roman character to its value. I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Scan from right to left. Keep track of the "previous" value. If current < previous, subtract current. Otherwise, add current.

3

Why scan right to left: when you see a smaller value before a larger one (e.g., IV), you know at the larger value that the previous one should be subtracted. By scanning right to left, you naturally handle this: I is added, then V is encountered and I is subtracted, giving 5 - 1 = 4.

4

Edge cases: single character (e.g., "V" = 5). All additive (e.g., "LVIII" = 58). All subtractive pairs (e.g., "IV" = 4, "XC" = 90, "CD" = 400, "CM" = 900). Maximum value "MMMCMXCIX" = 3999.

Optimal Approach

Step 1: Create a hash map for Roman character values.
Step 2: Initialize result = 0, prev_value = 0.
Step 3: Scan the string from right to left:

  • Get the value of the current character
  • If current value < prev_value, subtract it from result
  • Otherwise, add it to result
  • Set prev_value = current value
    Step 4: Return result.

Walkthrough for "MCMXCIV" (1994):

  • V(5): add -> result=5
  • I(1): 1<5, subtract -> result=4
  • C(100): 100>1, add -> result=104
  • X(10): 10<100, subtract -> result=94
  • M(1000): 1000>10, add -> result=1094
  • C(100): 100<1000, subtract -> result=994
  • M(1000): 1000>100, add -> result=1994.

Time: O(n) -- single pass through the string. Space: O(1) -- only a few variables.

Scan from right to left and compare with the previous value

sMCMXCIV0123456currStart at the last letter V = 5. Nothing before it yet, soadd 5.sMCMXCIV0123456currI = 1 is less than the previous 5, so subtract it: 5 − 1 =4.sMCMXCIV0123456currC = 100 is less than the previous 1000, so subtract it:1094 − 100 = 994.if val < prev: result -= valelse: result += valprev = val
Going right to left, a value smaller than the one just seen is subtracted; otherwise it is added.
ComplexityTime O(n)Space O(1)We touch each character once in a single pass, and we only keep a few variables plus a fixed 7-entry lookup table.

What Trips People Up in Real Interviews

1

Scanning left to right and trying to detect subtractive notation. This requires looking ahead at the next character, which adds complexity. Right-to-left scanning handles subtractive notation naturally without lookahead.

2

Forgetting that only I, X, C, and M can be subtracted. You can subtract I from V or X, X from L or C, C from D or M. You cannot subtract V from anything. The six subtractive patterns are: IV, IX, XL, XC, CD, CM.

3

Hardcoding all possible values instead of using a hash map. While a hash map is cleaner, some candidates list 15 if/else branches (I, II, III, IV, V, ...). This is error-prone and hard to maintain.

4

Not handling the edge case of a single character input. The right-to-left scan works fine with one character, but some implementations assume at least two characters and crash.

5

Confusing the direction of scanning. Left-to-right requires special handling for subtractive pairs (look ahead). Right-to-left naturally handles subtractive notation because you encounter the smaller value first.

Solution Code

O(n) time
def romanToInt(s):
    roman = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
    result = 0
    prev = 0
    for ch in reversed(s):
        val = roman[ch]
        if val < prev:
            result -= val
        else:
            result += val
        prev = val
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Roman to Integer problem?

Convert a Roman numeral string to an integer. Roman numerals use letters to represent values, with subtractive notation (e.g., IV = 4, IX = 9). This problem tests your ability to handle special cases and scan strings in reverse.

How do you solve Roman to Integer?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Roman to Integer?

Roman to Integer is asked at Google, Microsoft, Oracle, Uber, Bloomberg. It is a easy difficulty problem.

What are common mistakes on Roman to Integer?
  • Scanning left to right and trying to detect subtractive notation. This requires looking ahead at the next character, which adds complexity. Right-to-left scanning handles subtractive notation naturally without lookahead.
  • Forgetting that only I, X, C, and M can be subtracted. You can subtract I from V or X, X from L or C, C from D or M. You cannot subtract V from anything. The six subtractive patterns are: IV, IX, XL, XC, CD, CM.
  • Hardcoding all possible values instead of using a `hash map`. While a `hash map` is cleaner, some candidates list 15 if/else branches (I, II, III, IV, V, ...). This is error-prone and hard to maintain.
  • Not handling the edge case of a single character input. The right-to-left scan works fine with one character, but some implementations assume at least two characters and crash.
  • Confusing the direction of scanning. Left-to-right requires special handling for subtractive pairs (look ahead). Right-to-left naturally handles subtractive notation because you encounter the smaller value first.