Integer to Roman
Medium About 15 min Free, no account needed
Write Roman numerals the way Romans did - greedily.
The brief
Convert an integer (1..3999) to its Roman numeral. The subtractive forms
(IV IX XL XC CD CM) are mandatory: 4 is IV, never IIII.
Emitting one symbol per decimal digit turns 4, 9, 40, 90, 400 and
900 into a pile of special cases that is easy to get subtly wrong. The hidden
tests sit exactly on those boundaries (444, 900, 3999). There is a
formulation in which the subtractive forms need no special handling at all.
Starter code
using System;
using System.Text;
public class Solution
{
// TODO: return the Roman numeral for the given integer (1..3999).
public string IntToRoman(int num)
{
throw new NotImplementedException();
}
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
What it practices
- Greedy algorithms
- Make the locally best choice when an exchange argument proves it stays globally optimal.
- String processing
- Parse, scan and match text character by character with correct edge cases.
Related katas
- Text Justification Hard Free Greedily pack words into fully justified lines, spaces and all.
- Boats to Save People Medium Free Pair people into two-person boats under a weight limit, using as few boats as possible.
- Container With Most Water Medium Free Pick two lines that, with the x-axis, hold the most water.
- Decode String Medium Free Expand k[encoded] patterns, nesting included.