Skip to main content

13. Roman to Integer

easyAsked at JPMorgan

Convert a Roman-numeral string to its integer value. JPMorgan asks this on Software Engineer Programme phone screens because the subtraction rule (IV = 4, IX = 9) makes it the smallest interesting case-by-case parser — a clean check on careful conditional handling.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in JPMorgan loops.

  • Glassdoor (2026-Q1)JPMorgan SDE phone-screen reports list this as a recurring parser warm-up.
  • LeetCode (2026-Q1)Tagged JPMorgan on the LeetCode company tag page.
  • Blind (2025-10)JPMC SWE-I write-up cites Roman to Integer as a recurring phone screen.

Problem

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M, with values 1, 5, 10, 50, 100, 500, and 1000. 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. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I before V or X, X before L or C, C before D or M. Given a Roman numeral, convert it to an integer.

Constraints

  • 1 <= s.length <= 15
  • s contains only the 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

Input
s = "III"
Output
3

Example 2

Input
s = "LVIII"
Output
58

Explanation: L = 50, V = 5, III = 3.

Example 3

Input
s = "MCMXCIV"
Output
1994

Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

Approaches

1. Right-to-left walk with last-seen pivot

Walk the string right-to-left. Add the current symbol's value if it is >= the maximum seen so far; subtract otherwise.

Time
O(n)
Space
O(1)
function romanToIntRtL(s) {
  const v = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
  let total = 0;
  let maxSeen = 0;
  for (let i = s.length - 1; i >= 0; i--) {
    const cur = v[s[i]];
    if (cur >= maxSeen) {
      total += cur;
      maxSeen = cur;
    } else {
      total -= cur;
    }
  }
  return total;
}

Tradeoff: Single pass and O(1) extra. Elegant but the rule 'smaller than max seen' takes a second to explain; many candidates prefer the look-ahead version below.

2. Left-to-right with one-character look-ahead (optimal, most readable)

Walk left-to-right. If the current symbol is smaller than the next, subtract; otherwise add.

Time
O(n)
Space
O(1)
function romanToInt(s) {
  const v = { 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 cur = v[s[i]];
    const next = i + 1 < s.length ? v[s[i + 1]] : 0;
    if (cur < next) total -= cur;
    else total += cur;
  }
  return total;
}

Tradeoff: Most readable answer — the look-ahead reads exactly like the spoken rule. JPMorgan interviewers tend to prefer this version because the intent is obvious from the code.

JPMorgan-specific tips

JPMorgan interviewers grade the look-ahead variant slightly higher than the right-to-left variant on this problem because the code reads like the rule. Lead with: 'a Roman numeral is the sum of its values, except a smaller symbol just before a larger one means subtract.' Then write the look-ahead loop.

Common mistakes

  • Treating each two-character subtraction pair (IV, IX, XL, XC, CD, CM) as a special case with string slicing — works but is more code than the look-ahead.
  • Forgetting the out-of-bounds guard on the look-ahead at the last character.
  • Building a Map<string,int> for two-character pairs and a separate one for single — over-engineered.
  • Walking right-to-left and forgetting to update the maximum-seen on the >= case.

Follow-up questions

An interviewer at JPMorgan may pivot to one of these next:

  • Integer to Roman (LC 12) — the reverse problem. Greedy by descending value with the subtractive forms in the table.
  • Validate a Roman numeral string (no LC equivalent).
  • What if the alphabet were extended (e.g. Unicode for 5000, 10000)?
  • Compare two Roman numerals without first converting them.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the look-ahead approach work?

The Roman-numeral subtraction rule says a smaller symbol immediately to the left of a larger one is subtractive. So walking left-to-right, you only need to look one character ahead: if the current is strictly smaller than the next, subtract; otherwise add. The rule is local — it never spans more than two characters.

Can you do it in one line?

Yes, by transforming the string with regex replacements ('IV'->'4', 'IX'->'9', ...) and then summing. Cute but heavier and harder to explain. The look-ahead loop is the answer JPMorgan expects.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Roman to Integer and other JPMorgan interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →