‹ DS&A interview · Socratic
DSA · Strings · #153

Roman to Integer

Module 62 · difficulty 2/5·30:00starts on first keystroke

Roman numerals use seven symbols: `I=1`, `V=5`, `X=10`, `L=50`, `C=100`, `D=500`, `M=1000`. Numbers are written largest to smallest left to right and added together — except for six subtractive cases (`IV`, `IX`, `XL`, `XC`, `CD`, `CM`) where a smaller symbol placed before a larger one is subtracted. Implement `romanToInt(s)` that takes a valid Roman numeral string `s` and returns its integer value.

Examples
  • s = "III" 3I + I + I = 3.
  • s = "LVIII" 58L=50, V=5, III=3.
  • s = "MCMXCIV" 1994M=1000, CM=900, XC=90, IV=4.
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].
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.