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
What is the output of the following Java code that converts a Roman numeral string to an integer?
DSA Java
import java.util.*; public class RomanToInt { public static int romanToInt(String s) { Map<Character, Integer> map = new HashMap<>(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); int result = 0; for (int i = 0; i < s.length(); i++) { int value = map.get(s.charAt(i)); if (i + 1 < s.length() && value < map.get(s.charAt(i + 1))) { result -= value; } else { result += value; } } return result; } public static void main(String[] args) { System.out.println(romanToInt("MCMXCIV")); } }
Attempts:
2 left
💡 Hint
Remember that when a smaller value precedes a larger value, you subtract it.
✗ 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 smaller numeral when it appears before a larger numeral?
Attempts:
2 left
💡 Hint
Think about how Romans wrote numbers like 4 or 9.
✗ Incorrect
Romans used subtractive notation to write numbers like 4 as IV (5 - 1) instead of IIII to avoid repeating the same symbol four times.
🔧 Debug
advancedIdentify the Bug in Roman to Integer Code
What error will occur when running this Java code snippet for Roman to Integer conversion?
DSA Java
import java.util.*; public class RomanToIntBug { public static int romanToInt(String s) { Map<Character, Integer> map = new HashMap<>(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); int result = 0; for (int i = 0; i <= s.length(); i++) { int value = map.get(s.charAt(i)); if (i + 1 < s.length() && value < map.get(s.charAt(i + 1))) { result -= value; } else { result += value; } } return result; } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop uses i <= s.length(), which causes s.charAt(i) to access an index equal to string length, causing ArrayIndexOutOfBoundsException.
🚀 Application
advancedResult After Multiple Roman Numeral Conversions
Given the following Roman numerals: ["III", "IV", "IX", "LVIII"], what is the sum of their integer conversions?
Attempts:
2 left
💡 Hint
Convert each numeral individually and add the results.
✗ Incorrect
III = 3, IV = 4, IX = 9, LVIII = 58; sum = 3 + 4 + 9 + 58 = 74
❓ Predict Output
expertOutput of Modified Roman to Integer with Invalid Input
What will be the output of this Java code when given an invalid Roman numeral string "IM"?
DSA Java
import java.util.*; public class RomanToIntInvalid { public static int romanToInt(String s) { Map<Character, Integer> map = new HashMap<>(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); int result = 0; for (int i = 0; i < s.length(); i++) { int value = map.getOrDefault(s.charAt(i), -1); if (value == -1) return -1; // invalid character if (i + 1 < s.length() && value < map.getOrDefault(s.charAt(i + 1), 0)) { result -= value; } else { result += value; } } return result; } public static void main(String[] args) { System.out.println(romanToInt("IM")); } }
Attempts:
2 left
💡 Hint
The code does not validate subtractive rules strictly, only invalid characters.
✗ Incorrect
The code treats 'I' as 1 and 'M' as 1000. Since 1 < 1000, it subtracts 1 then adds 1000, resulting in 999. But the code returns 999, not -1, because 'I' and 'M' are valid characters.
