Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the integer value of a single Roman numeral character.
DSA Go
func value(r byte) int {
switch r {
case 'I':
return [1]
case 'V':
return 5
case 'X':
return 10
case 'L':
return 50
case 'C':
return 100
case 'D':
return 500
case 'M':
return 1000
default:
return 0
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for 'I'.
Confusing 'I' with 'V' or 'X'.
✗ Incorrect
The Roman numeral 'I' represents the integer 1.
2fill in blank
mediumComplete the code to iterate through the Roman numeral string correctly.
DSA Go
func romanToInt(s string) int {
total := 0
for i := 0; i < [1]; i++ {
// logic here
}
return total
}Attempts:
3 left
💡 Hint
Common Mistakes
Using len(s) - 1 as loop condition, which misses the last character.
Using cap(s), which is not valid for strings.
✗ Incorrect
We iterate from 0 to len(s)-1, so the loop condition is i < len(s).
3fill in blank
hardFix the error in the condition to check if the current Roman numeral is less than the next one.
DSA Go
if i+1 < len(s) && value(s[i]) [1] value(s[i+1]) { total -= value(s[i]) } else { total += value(s[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causing wrong subtraction logic.
Using '==' which does not handle subtraction cases.
✗ Incorrect
If the current value is less than the next, we subtract it according to Roman numeral rules.
4fill in blank
hardFill both blanks to complete the function that converts Roman numerals to integers.
DSA Go
func romanToInt(s string) int {
total := 0
for i := 0; i < [1]; i++ {
if i+1 < len(s) && value(s[i]) [2] value(s[i+1]) {
total -= value(s[i])
} else {
total += value(s[i])
}
}
return total
}Attempts:
3 left
💡 Hint
Common Mistakes
Looping only till len(s)-1 causing last character to be skipped.
Using '>' instead of '<' in subtraction condition.
✗ Incorrect
Loop runs till i < len(s) and subtraction happens when current value is less than next.
5fill in blank
hardFill all three blanks to create a map of Roman numerals and use it in the conversion function.
DSA Go
var romanMap = map[byte]int{
'I': [1],
'V': [2],
'X': [3],
}
func romanToInt(s string) int {
total := 0
for i := 0; i < len(s); i++ {
if i+1 < len(s) && romanMap[s[i]] < romanMap[s[i+1]] {
total -= romanMap[s[i]]
} else {
total += romanMap[s[i]]
}
}
return total
}Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect mapping values causing wrong results.
Not using the map for value lookup.
✗ Incorrect
Roman numerals I=1, V=5, X=10 are mapped correctly for conversion.
