Skip to content
MediumStringsAI interview only

Integer to Roman

Asked atamazonmicrosoftgoogleadobebloombergapple

01 · Problem

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000.

Numbers are written from the largest value to the smallest by adding symbols. To avoid four identical symbols in a row, six subtractive pairs are used instead: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900. Only these six pairs are allowed, and the symbols I, X, C, M may repeat at most three times in a row while V, L, D never repeat.

Given an integer num, return its standard Roman numeral representation as an uppercase string. Every input has exactly one valid representation under these rules.

02 · Examples

Example 01
Input
num = 44
Output
"XLIV"

40 = XL, 4 = IV, so 44 = "XLIV".

Example 02
Input
num = 2024
Output
"MMXXIV"

2000 = MM, 20 = XX, 4 = IV, so 2024 = "MMXXIV".

Example 03
Input
num = 944
Output
"CMXLIV"

900 = CM, 40 = XL, 4 = IV, so 944 = "CMXLIV".

03 · Constraints

  • 011 <= num <= 3999
  • 02Use only the symbols I, V, X, L, C, D and M
  • 03Use the subtractive forms IV, IX, XL, XC, CD and CM instead of four repeated symbols

04 · Optimal complexity

Time
O(1)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.