13. Roman to Integer
easyAsked at BloombergConvert a Roman-numeral string to an integer. Bloomberg uses this to test whether you can handle the subtractive notation (IV, IX, XL, XC, CD, CM) elegantly — the obvious solution has six special cases, the elegant one has none.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Bloomberg loops.
- Glassdoor (2026-Q1)— Bloomberg SWE phone-screen reports cite Roman to Integer as a recurring warm-up.
- Blind (2025-12)— Bloomberg new-grad reports mention this as the alternative to Two Sum on the phone screen.
Problem
Roman numerals are represented by seven different symbols: I (1), V (5), X (10), L (50), C (100), D (500), M (1000). Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. The same principle applies to nine (IX), forty (XL), ninety (XC), four hundred (CD), and nine hundred (CM). Given a Roman numeral, convert it to an integer.
Constraints
1 <= s.length <= 15s contains only characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').It is guaranteed that s is a valid Roman numeral in the range [1, 3999].
Examples
Example 1
s = "III"3Example 2
s = "LVIII"58Explanation: L = 50, V= 5, III = 3.
Example 3
s = "MCMXCIV"1994Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Approaches
1. Look-ahead subtraction (elegant)
Scan left-to-right. If current numeral is smaller than the next, subtract it. Otherwise add.
- Time
- O(n)
- Space
- O(1)
function romanToInt(s) {
const val = { 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 curr = val[s[i]];
const next = val[s[i + 1]] || 0;
total += curr < next ? -curr : curr;
}
return total;
}Tradeoff: No special-case branches. Bloomberg interviewers strongly prefer this — it shows you understand the invariant (smaller-before-larger = subtractive) rather than memorizing the six pairs.
2. Hardcoded pair lookup
Replace the six subtractive pairs first, then sum the remaining single-char values.
- Time
- O(n)
- Space
- O(1)
function romanToIntPairs(s) {
const pairs = { IV: 4, IX: 9, XL: 40, XC: 90, CD: 400, CM: 900 };
const single = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
let total = 0;
for (const [k, v] of Object.entries(pairs)) {
while (s.includes(k)) { total += v; s = s.replace(k, ''); }
}
for (const ch of s) total += single[ch];
return total;
}Tradeoff: Works but uses string mutation. Bloomberg's rubric grades the look-ahead version higher because it generalizes to any subtractive notation.
Bloomberg-specific tips
Bloomberg interviewers explicitly grade whether you can avoid hardcoding the six subtractive pairs. The look-ahead trick is the canonical answer. State 'when a smaller numeral comes before a larger, it's subtractive' BEFORE coding — this is the insight they're checking for.
Common mistakes
- Hardcoding all six subtractive pairs when the look-ahead invariant captures them in one rule.
- Going out of bounds at the last character — use 0 as the implicit next value.
- Confusing the order (IX is 9, not 11).
Follow-up questions
An interviewer at Bloomberg may pivot to one of these next:
- Integer to Roman (LC 12) — reverse direction with greedy subtraction.
- Excel Sheet Column Number (LC 171) — base-26 conversion in the same shape.
- Roman numeral arithmetic — add two Roman numerals.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the look-ahead trick the canonical answer?
Because it captures the entire Roman-numeral grammar in one if-statement. Hardcoding the six pairs works but doesn't generalize and is brittle if the alphabet expands.
What's the trickiest edge case?
The last character. Without a sentinel, you can read past the end. Using val[s[i+1]] || 0 elegantly handles it because val['undefined'] is undefined which OR-shortcuts to 0.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Roman to Integer and other Bloomberg interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →