Challenge - 5 Problems
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Roman to Integer Conversion Function
What is the output of the following C code when the input Roman numeral is "MCMXCIV"?
DSA C
int romanToInt(char * s) { int map[26] = {0}; map['I' - 'A'] = 1; map['V' - 'A'] = 5; map['X' - 'A'] = 10; map['L' - 'A'] = 50; map['C' - 'A'] = 100; map['D' - 'A'] = 500; map['M' - 'A'] = 1000; int total = 0; int i = 0; while (s[i]) { int value = map[s[i] - 'A']; int nextValue = map[s[i+1] - 'A']; if (nextValue > value) { total += (nextValue - value); i += 2; } else { total += value; i++; } } return total; } int main() { char roman[] = "MCMXCIV"; int result = romanToInt(roman); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Remember that when a smaller value precedes a larger value, you subtract the smaller from the larger.
✗ Incorrect
The Roman numeral MCMXCIV is converted as follows: M (1000) + CM (900) + XC (90) + IV (4) = 1994.
❓ Predict Output
intermediate2:00remaining
Output of Roman to Integer Conversion with Repeated Numerals
What is the output of the following C code when the input Roman numeral is "III"?
DSA C
int romanToInt(char * s) { int map[26] = {0}; map['I' - 'A'] = 1; map['V' - 'A'] = 5; map['X' - 'A'] = 10; map['L' - 'A'] = 50; map['C' - 'A'] = 100; map['D' - 'A'] = 500; map['M' - 'A'] = 1000; int total = 0; int i = 0; while (s[i]) { int value = map[s[i] - 'A']; int nextValue = map[s[i+1] - 'A']; if (nextValue > value) { total += (nextValue - value); i += 2; } else { total += value; i++; } } return total; } int main() { char roman[] = "III"; int result = romanToInt(roman); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Repeated numerals add their values.
✗ Incorrect
Each 'I' is 1, so III = 1 + 1 + 1 = 3.
🔧 Debug
advanced2:00remaining
Identify the Error in Roman to Integer Conversion Code
What error will the following C code produce when run with input "VI"?
DSA C
int romanToInt(char * s) { int map[26] = {0}; map['I' - 'A'] = 1; map['V' - 'A'] = 5; map['X' - 'A'] = 10; map['L' - 'A'] = 50; map['C' - 'A'] = 100; map['D' - 'A'] = 500; map['M' - 'A'] = 1000; int total = 0; int i = 0; while (s[i]) { int value = map[s[i] - 'A']; int nextValue = map[s[i+1] - 'A']; if (nextValue > value) { total += (nextValue - value); i += 2; } else { total += value; i++; } } return total; } int main() { char roman[] = "VI"; int result = romanToInt(roman); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check how the code accesses s[i+1] without verifying if it exists.
✗ Incorrect
When i is at the last character, s[i+1] is out of bounds, causing undefined behavior or segmentation fault.
❓ Predict Output
advanced2:00remaining
Output of Roman to Integer Conversion with Mixed Numerals
What is the output of the following C code when the input Roman numeral is "XLII"?
DSA C
int romanToInt(char * s) { int map[26] = {0}; map['I' - 'A'] = 1; map['V' - 'A'] = 5; map['X' - 'A'] = 10; map['L' - 'A'] = 50; map['C' - 'A'] = 100; map['D' - 'A'] = 500; map['M' - 'A'] = 1000; int total = 0; int i = 0; while (s[i]) { int value = map[s[i] - 'A']; int nextValue = map[s[i+1] - 'A']; if (nextValue > value) { total += (nextValue - value); i += 2; } else { total += value; i++; } } return total; } int main() { char roman[] = "XLII"; int result = romanToInt(roman); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
XL is 40, II is 2, sum them.
✗ Incorrect
XL is 50 - 10 = 40, plus II (1 + 1) = 2, total 42.
🧠 Conceptual
expert2:00remaining
Why does the Roman to Integer Conversion Algorithm Check Next Character?
Why does the Roman to Integer conversion algorithm check the next character's value when processing the current character?
Attempts:
2 left
💡 Hint
Roman numerals use subtractive notation when a smaller numeral precedes a larger one.
✗ Incorrect
If the next numeral is larger, the current numeral's value is subtracted; otherwise, it is added.
