InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Math & Geometry

Integer to Roman

medium Original ↗ 00:00

Problem

Roman numerals use seven symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Numbers are built by writing symbols from largest value to smallest and concatenating them, with six subtractive forms to avoid four-in-a-row: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900. Given an integer num, convert it to its Roman numeral string.

Examples

  • num = 3"III" — three ones.
  • num = 58"LVIII" — 50 (L) + 5 (V) + 3 (III).
  • num = 1994"MCMXCIV" — 1000 (M) + 900 (CM) + 90 (XC) + 4 (IV).

Constraints

  • 1 <= num <= 3999

Think about it first

Hint 1 Include the six subtractive pairs as if they were their own "symbols" with values 900, 400, 90, 40, 9, 4. Then every number is just a sum of values drawn from a fixed list.
Hint 2 Process the value/symbol pairs from largest to smallest. Greedily take as many copies of the current symbol as fit, subtract, and move on — this is optimal because Roman numerals are constructed exactly this way.
Hint 3 `divmod(num, value)` gives you both the repeat count and the remainder in one step. Alternatively, precompute per-digit tables (thousands, hundreds, tens, ones) and index into them.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug