← Create New Paste

Roman Numeral Conversion in Coding Interviews: Integer to Roman and Back

4 views
Roman numeral conversion looks like a warm-up question, and that is exactly why interviewers like it. It is short enough to finish in fifteen minutes, but it quickly shows whether you can spot a clean greedy rule, handle special cases without a pile of if-statements, and test your own code. Here is how to approach both directions calmly. Know the rules before you code There are seven symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Numerals are normally written from largest to smallest and added up. The twist is subtractive notation: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900. Say these rules out loud at the start. Confirming the input range (usually 1 to 3999) is also worth doing, because it tells the interviewer you think about constraints before writing loops. Integer to Roman: a greedy table The neat trick is to treat the six subtractive pairs as ordinary symbols. Build one ordered list of value-symbol pairs: 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. Then walk the list from the top. While the number is at least the current value, append the symbol and subtract the value. Because the table already contains 900, 400, 90, 40, 9 and 4, you never need a special case. Time is effectively constant for the bounded range, and you can explain why greedy works here: each value in the table is the largest piece that can legally start the remaining numeral. Roman to Integer: look one step ahead Going the other way, scan left to right with a small map of symbol values. If the current symbol is smaller than the next one, subtract it; otherwise add it. For example, in MCMXCIV you add M (1000), subtract C because M follows, add M, subtract X, add C, subtract I, add V, giving 1994. An equally good variant scans right to left and subtracts whenever a value is smaller than the largest seen so far. Pick one and explain it; don't switch halfway. Test like you mean it Walk through 3, 4, 9, 14, 40, 58, 1994 and 3999. Point out that the input can be assumed valid if the interviewer agrees; if they want validation, mention rejecting strings like IIII or IC and say how you would check them. Interviewers often care more about this conversation than about the code itself. Common mistakes - Hard-coding every digit position with nested conditionals instead of one table. - Forgetting the subtractive pairs in the table, which leads to IIII instead of IV. - Going off the end of the string when looking ahead in the Roman-to-integer direction. Practising out loud Easy problems become hard when you freeze on a live call, so practise narrating the table idea and your test cases before the real thing. If you would like support during the interview itself, Linkjob AI (https://linkjob.ai) gives real-time help during live job interviews and technical interviews. It can help you keep your explanation structured while you code.
Created Nov 17, 2026 00:32 Expires Nov 17, 2027 00:32