# The Roman Converter

> Roman numerals decoded.

Canonical URL: <https://datadriven.io/problems/the_roman_converter>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a Roman numeral string using symbols I=1, V=5, X=10, L=50, C=100, D=500, M=1000, return its integer value. When a smaller symbol precedes a larger one (IV, IX, XL, XC, CD, CM), subtract it from the larger one.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **pattern recognition with lookup tables** and the subtraction rule in Roman numerals. It checks whether candidates can handle the exception case (IV, IX, XL, etc.) elegantly within a left-to-right scan.

---

### Break down the requirements

#### Step 1: Build a symbol-to-value mapping

Map each Roman numeral character to its integer value: I=1, V=5, X=10, L=50, C=100, D=500, M=1000.

#### Step 2: Scan left to right

For each character, look up its value and add it to the total.

#### Step 3: Handle the subtraction rule

If the current value is less than the next value, subtract it instead of adding. This handles cases like IV (4) and IX (9).

---

### The solution

**Left-to-right scan with subtraction lookahead**

```python
def roman_to_int(s):
    values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    total = 0
    for i in range(len(s)):
        current = values[s[i]]
        if i + 1 < len(s) and current < values[s[i + 1]]:
            total -= current
        else:
            total += current
    return total
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the length of the Roman numeral string.
> 
> **Space:** O(1). The lookup table is fixed size.

> **Interviewers Watch For**
>
> Is your subtraction rule clean? Comparing the current value with the next value is the standard technique. Candidates who build a list of special two-character combinations (IV, IX, etc.) produce correct but verbose code.

> **Common Pitfall**
>
> Processing right to left without adjusting the logic. It works but is less intuitive. Left-to-right with lookahead is the standard approach.

---

## Common follow-up questions

- How would you convert an integer back to a Roman numeral? _(Tests the reverse mapping with greedy subtraction of the largest possible values.)_
- What if the input contained invalid characters? _(Tests input validation before processing.)_
- What if Roman numerals used a different notation for subtraction? _(Tests parameterizing the subtraction rule.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_roman_converter)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.