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 Go code that converts a Roman numeral string to an integer?
DSA Go
package main import "fmt" func romanToInt(s string) int { values := map[byte]int{'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} total := 0 for i := 0; i < len(s); i++ { if i+1 < len(s) && values[s[i]] < values[s[i+1]] { total -= values[s[i]] } else { total += values[s[i]] } } return total } func main() { fmt.Println(romanToInt("MCMXCIV")) }
Attempts:
2 left
💡 Hint
Remember that when a smaller value precedes a larger one, you subtract it.
✗ Incorrect
The Roman numeral "MCMXCIV" is converted as follows:
- M = 1000
- CM = 900 (100 before 1000 means subtract 100)
- XC = 90 (10 before 100 means subtract 10)
- IV = 4 (1 before 5 means subtract 1)
Adding these: 1000 + 900 + 90 + 4 = 1994.
❓ Predict Output
intermediateResult of Roman to Integer Conversion with Repeated Characters
What integer does the following Go code output when converting the Roman numeral "III"?
DSA Go
package main import "fmt" func romanToInt(s string) int { values := map[byte]int{'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} total := 0 for i := 0; i < len(s); i++ { if i+1 < len(s) && values[s[i]] < values[s[i+1]] { total -= values[s[i]] } else { total += values[s[i]] } } return total } func main() { fmt.Println(romanToInt("III")) }
Attempts:
2 left
💡 Hint
Repeated 'I' characters add up.
✗ Incorrect
"III" is three times 'I', each worth 1, so total is 3.
🔧 Debug
advancedIdentify the Error in Roman to Integer Conversion Code
What error will the following Go code produce when run?
DSA Go
package main import "fmt" func romanToInt(s string) int { values := map[byte]int{'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} total := 0 for i := 0; i < len(s); i++ { if i+1 < len(s) && values[s[i]] < values[s[i+1]] { total -= values[s[i]] } else { total += values[s[i]] } } return total } func main() { fmt.Println(romanToInt("IX")) }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop uses i <= len(s), which causes an index out of range error when i equals len(s) because s[len(s)] is invalid.
❓ Predict Output
advancedOutput of Roman to Integer Conversion for Complex Numeral
What is the output of the following Go code converting the Roman numeral "XLII"?
DSA Go
package main import "fmt" func romanToInt(s string) int { values := map[byte]int{'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} total := 0 for i := 0; i < len(s); i++ { if i+1 < len(s) && values[s[i]] < values[s[i+1]] { total -= values[s[i]] } else { total += values[s[i]] } } return total } func main() { fmt.Println(romanToInt("XLII")) }
Attempts:
2 left
💡 Hint
Remember 'XL' means 40 (50 - 10).
✗ Incorrect
"XL" is 40 (50 - 10), plus "II" is 2, total 42.
🧠 Conceptual
expertWhy Does the Roman to Integer Algorithm Subtract in Some Cases?
Why does the Roman to Integer conversion algorithm subtract the value of a Roman numeral character when it appears before a larger value character?
Attempts:
2 left
💡 Hint
Think about how Roman numerals represent 4 and 9.
✗ Incorrect
Roman numerals use subtractive notation for certain numbers. For example, 'IV' means 5 - 1 = 4, and 'IX' means 10 - 1 = 9. The algorithm subtracts the smaller value when it appears before a larger one to handle this notation correctly.
