Skip to main content

13. Roman to Integer

easyAsked at Goldman Sachs

Convert a Roman numeral string to its integer value. Goldman Sachs uses this as a 10-minute warm-up because it rewards candidates who notice the 'subtractive pair' insight before writing code — interviewers grade the verbal articulation as much as the implementation.

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

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE phone-screen reports list Roman to Integer in the top-10 most-asked.
  • Blind (2025-12)Goldman Sachs Strats candidate reports Roman to Integer as the first warm-up on their HackerRank round.

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, 1000. Numerals are normally written largest-to-smallest left-to-right, but six cases use subtraction: IV (4), IX (9), XL (40), XC (90), CD (400), CM (900). 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. Pair lookup with two-char prefix check

Walk the string. At each position, if the current pair is in {IV, IX, XL, XC, CD, CM}, take its value and skip 2; otherwise take the single-char value.

Time
O(n)
Space
O(1)
function romanToInt2(s) {
  const single = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
  const pair = { IV: 4, IX: 9, XL: 40, XC: 90, CD: 400, CM: 900 };
  let i = 0, total = 0;
  while (i < s.length) {
    const two = s.substring(i, i + 2);
    if (pair[two] !== undefined) { total += pair[two]; i += 2; }
    else { total += single[s[i]]; i += 1; }
  }
  return total;
}

Tradeoff: Direct translation of the rules. Slightly verbose but explicit about the six subtractive cases. Mention it to show you understand the spec, then move to the cleaner version.

2. Right-to-left or left-to-right with peek (optimal)

Scan left to right. If the current value is less than the next value, subtract; otherwise add. This single rule subsumes all six subtractive pairs.

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 = v[s[i + 1]] || 0;
    total += cur < next ? -cur : cur;
  }
  return total;
}

Tradeoff: The 'if cur < next, subtract' insight collapses six special cases into one rule. This is the version Goldman is grading — interviewers love hearing you derive the rule before coding.

Goldman Sachs-specific tips

Goldman Sachs interviewers are listening for the moment you say 'instead of hard-coding the six subtractive pairs, I notice they're exactly the cases where the next numeral is larger.' That insight is the differentiator between a hire and a 'meh' on the rubric. Spend 60 seconds on the whiteboard mapping out 1994 = MCMXCIV before writing code.

Common mistakes

  • Hard-coding the six subtractive pairs and missing one (CD and CM are commonly forgotten).
  • Iterating right-to-left with a 'last seen' counter and getting the comparison direction wrong.
  • Forgetting that the last character has no 'next', requiring a default of 0 for the peek.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • Reverse: convert integer back to Roman. (Greedy descending-value match, slightly trickier subtractive case generation.)
  • Validate a Roman numeral — does the input follow the canonical rules? (Not all sequences of {I,V,X,L,C,D,M} are valid.)
  • What if you needed to handle Roman numerals up to 100000 using overline notation? (Same insight, expanded symbol table.)

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is left-to-right with peek the same as right-to-left with accumulator?

Yes — they're dual algorithms that produce the same answer in O(n). Goldman doesn't care which direction; they care that you noticed the 'cur < next means subtract' invariant rather than hard-coding 6 cases.

Why is the input guaranteed to be a valid numeral?

Because validating Roman numerals is itself a non-trivial subproblem (e.g. IIII is not valid but IIII is sometimes seen on clock faces). Goldman sometimes asks the validation question as a follow-up.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →