The Roman to Integer interview question asks you to convert a Roman numeral string (using symbols I, V, X, L, C, D, M) to its corresponding integer value. Roman numerals follow specific additive and subtractive rules: when a smaller value symbol appears before a larger one (e.g., IV = 4, IX = 9), it is subtracted rather than added. Your task is to parse the string and compute the correct integer result.
This problem is asked at Apple, Uber, Goldman Sachs, Microsoft, Meta, Amazon, LinkedIn, Google, Bloomberg, Adobe, and dozens more companies because it tests string parsing, lookup tables (hash maps), and the ability to handle rules with exceptions. Roman numeral conversion is a classic example of translating a domain-specific language into a numeric value — directly applicable to parsing DSLs, financial notation, and document numbering systems.
The pattern is left-to-right scan with a value lookup map. Build a map of Roman symbols to values: {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}. Iterate through the string. If the current symbol's value is less than the next symbol's value (subtractive case), subtract the current value from the total. Otherwise, add it. Process the last character as always additive.
Input: "MCMXCIV"
Result: 1994.
ord() arithmetic instead of a proper lookup map, making the code fragile.For the Roman to Integer coding problem, the math and hash table interview pattern is elegant: build the symbol-to-value map, scan left to right, subtract when current < next. This one-pass O(n) solution handles all subtractive cases uniformly. Interviewers at Goldman Sachs and Capital One sometimes ask the reverse (Integer to Roman) as a follow-up — know that direction too. Practice both on a few examples (XIV, XLII, CMXCIX) to build confidence.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Integer to Roman | Medium | Solve | |
| Fraction to Recurring Decimal | Medium | Solve | |
| Reconstruct Original Digits from English | Medium | Solve | |
| Maximum Manhattan Distance After K Changes | Medium | Solve | |
| Count Number of Texts | Medium | Solve |