Roman Numeral to Integer
Updated
Problem statement
Roman numerals use seven letters. Each letter stands for a value:
| Letter | Value |
|---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Letters are usually written from the largest value to the smallest, and their values are added. For example, CLXI is 100 + 50 + 10 + 1 = 161.
Six pairs use subtraction instead. In each pair, a smaller letter comes right before a larger one, and the pair is worth the larger value minus the smaller one:
IVis 4 andIXis 9.XLis 40 andXCis 90.CDis 400 andCMis 900.
You are given a string roman. It is the standard Roman numeral for a whole number from 1 to 3999. Standard means the thousands, hundreds, tens and ones are written in that order, and each digit uses its usual form. For example, 4 is IV (never IIII) and 90 is XC (never LXXXX).
Return the number that roman stands for.
Function
romanToInteger(roman: String) → intExamples
Example 1
roman = "XXVII"return = 27No letter is followed by a larger one, so just add: 10 + 10 + 5 + 1 + 1 = 27.
Example 2
roman = "XLIX"return = 49XL is a subtraction pair worth 40, and IX is worth 9. So the value is 40 + 9 = 49.
Example 3
roman = "MMCDXXI"return = 2421MM is 2000, CD is 400, XX is 20 and I is 1. The total is 2421.
Constraints
1 <= roman.length <= 15romanis the standard Roman numeral for a whole number from1to3999.
More Scale AI Phone Screen questions
- Four-Player Trick-Taking Card Game SimulationOA · PHONE SCREEN · Seen Aug 2026
- Task Processor: Dependencies and DeadlinesPHONE SCREEN · Seen Aug 2026
- Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Free Time for Every Meeting RoomPHONE SCREEN · Seen Apr 2026
- Free Windows Between Busy IntervalsOA · PHONE SCREEN · Seen Apr 2026
- Order Queries by Shortest Processing TimePHONE SCREEN · Seen Apr 2026
- Party Time by Town and Quiet Gaps by CityPHONE SCREEN · Seen Jan 2026
- Party Windows and Dead Zone TimePHONE SCREEN · Seen Oct 2025
- Overlap Duration of Two Time IntervalsPHONE SCREEN · Seen Jun 2025
- Multi-Head Attention BlockPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Parse, Normalize, and Combine PolynomialsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Rank Bipedal Dinosaurs by SpeedMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Count Sentences From Anagram SwapsAdobe · PHONE SCREENAdobe interview questions
- In-Memory URL ShortenerRevolut · PHONE SCREENRevolut interview questions
- Simplify a Formula with Known and Unknown VariablesBetter.com · PHONE SCREEN
- Reconstruct Digits from Jumbled English WordsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions