PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

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.

Input: "IX"

Expected Output: 9

Explanation: I (1) precedes X (10), so it is subtracted: 10 - 1 = 9.

Input: "LVIII"

Expected Output: 58

Explanation: L=50, V=5, III=3, all added: 50 + 5 + 3 = 58.

Input: "MCMXCIV"

Expected Output: 1994

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

Input: "I"

Expected Output: 1

Explanation: Single symbol edge case: I = 1.

Input: "MMMCMXCIX"

Expected Output: 3999

Explanation: Maximum valid value: MMM=3000, CM=900, XC=90, IX=9 = 3999.

Input: "XL"

Expected Output: 40

Explanation: X (10) precedes L (50), subtracted: 50 - 10 = 40.

Input: "CD"

Expected Output: 400

Explanation: C (100) precedes D (500), subtracted: 500 - 100 = 400.

Input: "MCDLXXVI"

Expected Output: 1476

Explanation: M=1000, CD=400, L=50, XX=20, V=5, I=1: 1000 + 400 + 50 + 20 + 5 + 1 = 1476.

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.
Last updated: Jul 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a FIFO Queue Using Two Stacks - Apple (medium)
  • Vertical Order Traversal of a Binary Tree - Apple (medium)
  • Minimal Unique Word Abbreviations - Apple (medium)
  • Minimum Cells to Bridge a Magic Grid - Apple (hard)
  • Find Common Prefix Across Strings - Apple (easy)