- Interview Questions
- /
- Problems
- /
- Roman to Integer
Roman to Integer
Convert a Roman numeral into an ordinary integer. The only real rule to get right is that a smaller symbol before a larger one is subtracted. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
Roman numerals use seven symbols:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Symbols are normally written from largest to smallest and added together, so "XVI" is 10 + 5 + 1 = 16. Six combinations instead use subtraction, where a smaller symbol placed right before a larger one is subtracted from it: IV (4), IX (9), XL (40), XC (90), CD (400), and CM (900).
Given a valid Roman numeral s, return its integer value.
Example 1
- Input
- s = "LXXIV"
- Output
- 74
L + X + X + IV = 50 + 10 + 10 + 4 = 74.
Example 2
- Input
- s = "CDXLIV"
- Output
- 444
CD + XL + IV = 400 + 40 + 4. Every digit uses a subtractive pair.
Example 3
- Input
- s = "MMXXVI"
- Output
- 2026
Constraints
- 1 ≤ s.length ≤ 15
- s contains only the characters I, V, X, L, C, D, M.
- s is a valid Roman numeral for a value from 1 to 3999.
Practice
Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Roman to Integer in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Roman to Integer
Pattern: Compare each symbol with the next
You don't need to list the six subtractive pairs. They all follow one rule: a symbol is subtracted exactly when the symbol after it is larger. In XIX, the I sits before an X, so it counts as -1; both Xs are followed by something smaller or nothing, so they count as +10.
That makes the whole conversion a single left-to-right pass with a lookup table. At each position, peek at the next symbol: if it's bigger, subtract the current value, otherwise add it.
Approach 1: Match the two-letter pairs first
Keep a table with all 13 tokens (the seven symbols plus the six subtractive pairs). At each position, try to match a two-character token first, then a single one, and advance by its length. Correct and explicit, but the table is longer and the matching logic has more branches.
Time O(n) · Space O(1)
Approach 2: Single pass comparing neighbors
- Map each symbol to its value.
- For each index
i, letvbe the value ofs[i]. Ifi + 1 < nand the value ofs[i + 1]is greater thanv, subtractvfrom the total; otherwise add it. - Return the total.
You can also scan right to left, subtracting a value whenever it's smaller than the largest seen so far; it's the same idea.
Time O(n) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Match the two-letter pairs first | O(n) | O(1) |
| Single pass comparing neighbors | O(n) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Roman to Integer solution in Python
def roman_to_int(s: str) -> int:
values = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
total = 0
for i, ch in enumerate(s):
v = values[ch]
if i + 1 < len(s) and values[s[i + 1]] > v:
total -= v
else:
total += v
return totalRoman to Integer solution in JavaScript
function romanToInt(s) {
const values = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
let total = 0;
for (let i = 0; i < s.length; i++) {
const v = values[s[i]];
if (i + 1 < s.length && values[s[i + 1]] > v) total -= v;
else total += v;
}
return total;
}Common mistakes
- Comparing with the previous symbol and forgetting to undo what you already added. If you go that route, a subtractive pair needs `+ current - 2 × previous`.
- Reading past the end of the string when peeking at `s[i + 1]` on the last character.
- Treating equal neighbors as subtractive. `XX` is 20; only a strictly larger next symbol triggers subtraction.
- Hard-coding only some of the six pairs, for example handling `IV` and `IX` but not `CD` or `CM`.
What interviewers look for
State the general rule before coding: "subtract when the next symbol is bigger." That replaces six special cases with one comparison, and interviewers notice when you generalize instead of enumerating.
This problem is usually a warm-up, so speed and cleanliness matter. Write the lookup table, the loop, and the boundary check, then trace one numeral that mixes adding and subtracting, like XCIV.
Follow-up questions
How would you convert an integer back into a Roman numeral?
Greedy: walk the 13 tokens from largest (M = 1000) to smallest (I = 1), including the subtractive pairs, and repeatedly append a token while its value fits in what remains. It always produces the standard form.
How would you validate that a string is a well-formed numeral?
Check the rules a converter ignores: I, X, C, M repeat at most three times in a row; V, L, D never repeat; only I, X, C can be subtracted, and only from the next two larger symbols. A regular expression like `^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$` captures all of it.
Does this rule hold for numbers above 3999?
Standard notation stops at 3999. Larger values historically used a bar over a symbol to multiply it by 1000; supporting that means extending the symbol table, and the neighbor-comparison rule still works on the extended values.
Companies that ask Roman to Integer
Roman to Integer is tagged by 133 companies in our interview dataset. The score shows how often it comes up at each company.
- Saic100%
- AMD100%
- Deltax100%
- Yahoo96.9%
- Crispr Therapeutics94%
- Hashicorp93%
- Appdirect93%
- Adidas91%
- Cerebras Systems90%
- Sap Concur89%
- Ch Robinson88%
- Neogen88%
- Slalom Build86%
- Thermo Fisher Scientific84%
- Outreach84%
- IBM82.9%
- Sonos81%
- Ifood81%
- Block80%
- Servicetitan80%
- Mollie79%
- Warnermedia78.1%
- Commscope78%
- Podium77%
and 109 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required