Integer to Roman solution explained
Integer to Roman is solved greedily: keep a table of the 13 values that have their own symbols, from 1000 (M) down through 900 (CM), 500 (D), 400 (CD) and so on to 1 (I), and repeatedly take the largest value that still fits, appending its symbols. Adding the six subtractive forms to the table is what makes the greedy choice correct. Because the input stops at 3999, the work is bounded by a constant; a place-value lookup (one array each for thousands, hundreds, tens and ones) does the same job with four reads.
By Evin JohnPublished 6 min read
The problem in plain words
Take a whole number from 1 to 3999 and spell it as a standard Roman numeral, the form used for a film's copyright year. Each decimal place is written separately, from thousands down to ones. Within a place, the digits 1 to 3 repeat the place's letter, 5 has its own letter, 6 to 8 put the 5-letter first and add ones after it, and the awkward digits 4 and 9 are written by placing a smaller letter before a larger one. So 4 is IV and 9 is IX, 40 is XL and 90 is XC, 400 is CD and 900 is CM. The output must be the standard short form; IIII or VIIII are not accepted.
| Number | Numeral | By place |
|---|---|---|
| 9 | IX | ones: 9 |
| 60 | LX | tens: 6 |
| 409 | CDIX | hundreds: 4, tens: 0 (nothing written), ones: 9 |
| 1666 | MDCLXVI | Every base letter once, in descending order |
| 1904 | MCMIV | thousands: 1, hundreds: 9, ones: 4 |
| 3888 | MMMDCCCLXXXVIII | The longest result in range, 15 letters |
What the interviewer wants to see
The core skill is recognizing that a greedy algorithm is safe here and being able to say why. Greedy fails for some coin systems, so "take the biggest piece that fits" is a claim, not a given. Interviewers also look at how you represent the data (an ordered list of value and symbol pairs, not a hash map whose order you then have to sort) and whether you notice that the table must include the six subtractive forms. It is common to get this right after Roman to Integer, and the contrast between the two directions is a good thing to mention.
Follow-ups
- Why is greedy correct here? Within each decimal place the 13 values cover every digit with the fewest symbols, and no symbol from a lower place can add up to a higher place's value without passing through that place's own symbols. Taking the largest fitting value never blocks a better choice later.
- Input of 0 or 4000. Neither has a standard numeral. Ask whether to raise an error or return an empty string.
- Make it fast for millions of calls. Precompute the four place arrays (or all 3999 answers) once; each call is then a few lookups.
- Write the inverse. Point to the neighbor-comparison rule in Roman to Integer, and use the two together as a round-trip test.
First version: additive, then fix the long runs
If you blank on the subtractive forms, this version still lands. Write the number using only M, D, C, L, X, V and I, as many of each as fit, so 1904 becomes MDCCCCIIII. Then replace each long run with its short form. The order of the replacements matters: rewrite the nines (DCCCC, LXXXX, VIIII) before the fours (CCCC, XXXX, IIII). In the wrong order, 1904 comes out as MDCDIV, because CCCC inside DCCCC was replaced first and the D was left stranded. That pitfall makes a nice thing to point out unprompted.
@@integer-to-roman/slow.py@@Expected version: greedy over 13 values
Put the subtractive forms in the table alongside the base letters, in descending order of value: 1000 M, 900 CM, 500 D, 400 CD, 100 C, 90 XC, 50 L, 40 XL, 10 X, 9 IX, 5 V, 4 IV, 1 I. Walk the table once. For each entry, divide the remaining number by its value; the quotient is how many copies of the symbols to append, and the remainder carries on to the next entry. Using divmod (or a loop that subtracts while the value fits) gives identical output; the division avoids repeating the comparison.
@@integer-to-roman/fast.py@@A second way skips the greedy reasoning altogether. Every decimal digit in every place has exactly one spelling, so list them: ten spellings for ones, ten for tens, ten for hundreds and four for thousands. Pull out each digit with division and remainder, and join four strings. It is the fastest and arguably the easiest to verify by eye, at the cost of typing out the arrays.
@@integer-to-roman/fast.js@@Cost of each approach
| Approach | Work per call | Memory | Trade-off |
|---|---|---|---|
| Additive letters, then replacements | A few passes over a string of at most 18 letters | O(1) | Easy to recall; replacement order is a trap |
| Greedy over 13 values | 13 divisions plus the output | O(1) | Needs the argument for why greedy is safe |
| Place-value arrays | 4 lookups plus the output | 34 short strings | No reasoning needed; more typing |
Edge cases
- 1 and 3999, the ends of the range (I and MMMCMXCIX).
- Numbers with zeros in the middle, such as 409 or 2006, where a place contributes nothing.
- Every 4 and 9 in each place, which is where wrong tables show up: 4, 9, 40, 90, 400, 900.
- Numbers whose digits are all 8, such as 3888, which give the longest output.
- Out-of-range input (0, negatives, 4000), which the problem excludes but which you should mention.
Common slips
- Leaving the subtractive forms out of the greedy table, which produces IIII and VIIII.
- Storing the table in an unordered map and iterating in whatever order it returns.
- Building the output with repeated string concatenation in a loop in a language where that copies the string each time. For at most 15 letters it does not matter, but say you would use a list and a join or a string builder.
- In the place-value version, forgetting the empty string at index 0 of each array, which shifts every digit by one.
How to explain it out loud
Fictional example
- Candidate
- I'll keep thirteen value and symbol pairs in descending order, including the subtractive ones like 900 as CM and 4 as IV, then take the largest that fits until the number reaches zero.
- Candidate
- Greedy isn't always safe, so here's why it is here: each decimal place is written on its own, and within a place these symbols spell every digit, so taking the biggest piece never forces a worse spelling later.
- Candidate
- Since the input is capped at 3999 it's constant time. To test it I'd round-trip every value from 1 to 3999 through a Roman-to-integer function and check the outputs are all different.
Related problems
Roman to Integer is the reverse conversion and makes a free test oracle for this one. Zigzag Conversion is another string-building task where a fixed pattern decides where each character goes. Fizz Buzz uses the same idea of a small ordered rule table driving the output.
Natively for practice
This is a problem where the explanation can matter as much as the code. In a mock round on Mac or Windows, Natively can read the question from a screenshot, let you compare the greedy table with the place-value arrays, and press you on why greedy is safe here. Looking at the transcript afterwards shows whether you actually stated that argument or only implied it.
Questions people ask
How do you convert an integer to a Roman numeral?
Keep the 13 values that have their own symbols, including 900 (CM), 400 (CD), 90 (XC), 40 (XL), 9 (IX) and 4 (IV), in descending order, and repeatedly append the largest symbol whose value still fits.
Why does the greedy approach work for Integer to Roman?
Each decimal place is written independently, and within a place the 13 values spell every digit. Taking the largest value that fits never forces a worse spelling later, so greedy gives the standard numeral.
What is the time complexity of Integer to Roman?
Constant, because the input is at most 3999: at most 13 table entries and at most 15 output letters. A place-value lookup needs four array reads.
What is the largest number Integer to Roman handles?
3999, written MMMCMXCIX. Standard Roman numerals have no letter above M, so the problem limits input to 1 through 3999.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
