Quick Overview

This question evaluates a candidate's ability to parse string input and apply conditional arithmetic logic to convert Roman numerals into integers. It tests string traversal skills and edge-case handling of subtractive notation, commonly used to assess foundational algorithmic thinking in coding interviews.

Convert a Roman Numeral to an Integer

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Convert a Roman Numeral to an Integer Roman numerals are written with seven symbols, each with a fixed integer value: | Symbol | Value | |--------|-------| | `I` | 1 | | `V` | 5 | | `X` | 10 | | `L` | 50 | | `C` | 100 | | `D` | 500 | | `M` | 1000 | A numeral is normally written from the largest-value symbol to the smallest, left to right, and its value is the **sum** of the symbol values. To avoid writing four identical symbols in a row, a smaller-value symbol placed immediately **before** a larger-value symbol is **subtracted** instead of added. Exactly six subtractive combinations are valid: - `IV` = 4 and `IX` = 9 - `XL` = 40 and `XC` = 90 - `CD` = 400 and `CM` = 900 Given a string `s` that is a valid Roman numeral, return its integer value. ## Examples - `s = "III"` -> `3` - `s = "IV"` -> `4` - `s = "LVIII"` -> `58` (`L` = 50, `V` = 5, `III` = 3) - `s = "MCMXCIV"` -> `1994` (`M` = 1000, `CM` = 900, `XC` = 90, `IV` = 4) ## Constraints - `1 <= s.length <= 15` - `s` contains only the characters `I`, `V`, `X`, `L`, `C`, `D`, `M`. - `s` is guaranteed to be a valid Roman numeral representing an integer in the range `[1, 3999]`. ## Required Implement a function that takes the string `s` and returns the corresponding integer.

Quick Answer: This question evaluates a candidate's ability to parse string input and apply conditional arithmetic logic to convert Roman numerals into integers. It tests string traversal skills and edge-case handling of subtractive notation, commonly used to assess foundational algorithmic thinking in coding interviews.

Roman numerals use seven symbols with fixed values: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A numeral is normally written largest-to-smallest, left to right, and its value is the sum of the symbol values. To avoid four identical symbols in a row, a smaller-value symbol placed immediately before a larger-value symbol is subtracted instead of added. The six valid subtractive combinations are IV=4, IX=9, XL=40, XC=90, CD=400, CM=900. Given a string `s` that is a valid Roman numeral, return its integer value. Examples: - s = "III" -> 3 - s = "IV" -> 4 - s = "LVIII" -> 58 (L=50, V=5, III=3) - s = "MCMXCIV" -> 1994 (M=1000, CM=900, XC=90, IV=4) Constraints: - 1 <= s.length <= 15 - s contains only the characters I, V, X, L, C, D, M. - s is guaranteed to be a valid Roman numeral in the range [1, 3999].

Constraints

  • 1 <= s.length <= 15
  • s contains only the characters I, V, X, L, C, D, M.
  • s is guaranteed to be a valid Roman numeral representing an integer in the range [1, 3999].

Examples

Input: "III"

Expected Output: 3

Explanation: Three I's, no subtraction: 1 + 1 + 1 = 3.

Input: "IV"

Expected Output: 4

Explanation: I (1) precedes V (5), so it is subtracted: 5 - 1 = 4.

Hints

  1. Map each symbol to its value. The tricky part is the six subtractive pairs (IV, IX, XL, XC, CD, CM).
  2. Instead of special-casing the six pairs, notice a simpler rule: scan left to right and compare each symbol with the one after it.
  3. If a symbol's value is less than the value of the symbol immediately to its right, subtract it; otherwise add it. This handles every subtractive case automatically.

Loading coding console...