Challenge - 5 Problems
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Roman to Integer Conversion Function
What is the output of the following C# code that converts a Roman numeral string to an integer?
DSA C#
using System; using System.Collections.Generic; class Program { static int RomanToInt(string s) { Dictionary<char, int> map = new Dictionary<char, int>() { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; int total = 0; for (int i = 0; i < s.Length; i++) { if (i + 1 < s.Length && map[s[i]] < map[s[i + 1]]) { total -= map[s[i]]; } else { total += map[s[i]]; } } return total; } static void Main() { Console.WriteLine(RomanToInt("MCMXCIV")); } }
Attempts:
2 left
💡 Hint
Remember that when a smaller Roman numeral appears before a larger one, it should be subtracted.
✗ Incorrect
The Roman numeral MCMXCIV is 1000 + (1000 - 100) + (100 - 10) + (5 - 1) = 1994.
🧠 Conceptual
intermediateUnderstanding Subtraction Rule in Roman Numerals
In Roman numeral conversion, why do we subtract the value of a numeral when it appears before a larger numeral?
Attempts:
2 left
💡 Hint
Think about how IV means 4, not 6.
✗ Incorrect
In Roman numerals, placing a smaller numeral before a larger one means subtracting the smaller from the larger, e.g., IV is 5 - 1 = 4.
🔧 Debug
advancedIdentify the Bug in Roman to Integer Conversion
What error will occur when running this code snippet for Roman numeral conversion?
DSA C#
using System; using System.Collections.Generic; class Program { static int RomanToInt(string s) { Dictionary<char, int> map = new Dictionary<char, int>() { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; int total = 0; for (int i = 0; i <= s.Length; i++) { if (i + 1 < s.Length && map[s[i]] < map[s[i + 1]]) { total -= map[s[i]]; } else { total += map[s[i]]; } } return total; } static void Main() { Console.WriteLine(RomanToInt("IX")); } }
Attempts:
2 left
💡 Hint
Check the loop condition and how the index is used.
✗ Incorrect
The loop uses i <= s.Length which causes an index out of range when accessing s[i] at the last iteration.
❓ Predict Output
advancedOutput of Modified Roman to Integer Code
What is the output of this modified Roman to Integer code when input is "XLII"?
DSA C#
using System; using System.Collections.Generic; class Program { static int RomanToInt(string s) { Dictionary<char, int> map = new Dictionary<char, int>() { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; int total = 0; int prev = 0; for (int i = s.Length - 1; i >= 0; i--) { int current = map[s[i]]; if (current < prev) { total -= current; } else { total += current; } prev = current; } return total; } static void Main() { Console.WriteLine(RomanToInt("XLII")); } }
Attempts:
2 left
💡 Hint
This code processes the string from right to left.
✗ Incorrect
XLII is 40 + 1 + 1 = 42. The code correctly subtracts X (10) because it is less than L (50) which was previous.
🧠 Conceptual
expertWhy Use a Dictionary for Roman Numeral Conversion?
Why is a dictionary (hash map) the best data structure to map Roman numerals to integers in conversion algorithms?
Attempts:
2 left
💡 Hint
Think about how often you need to find the value of each Roman character.
✗ Incorrect
A dictionary allows quick access to the integer value for each Roman numeral character, making the conversion efficient.
