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 Rust function when converting the Roman numeral
"MCMXCIV"?DSA Rust
fn roman_to_int(s: &str) -> i32 { let mut result = 0; let mut prev = 0; for c in s.chars().rev() { let value = match c { 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000, _ => 0, }; if value < prev { result -= value; } else { result += value; } prev = value; } result } fn main() { let roman = "MCMXCIV"; let integer = roman_to_int(roman); println!("{}", integer); }
Attempts:
2 left
💡 Hint
Remember that in Roman numerals, smaller values before larger ones mean subtraction.
✗ Incorrect
The Roman numeral MCMXCIV is 1000 + (1000-100) + (100-10) + (5-1) = 1994.
❓ Predict Output
intermediateOutput of Roman to Integer Conversion with Repeated Characters
What is the output of the Rust function when converting the Roman numeral
"III"?DSA Rust
fn roman_to_int(s: &str) -> i32 { let mut result = 0; let mut prev = 0; for c in s.chars().rev() { let value = match c { 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000, _ => 0, }; if value < prev { result -= value; } else { result += value; } prev = value; } result } fn main() { let roman = "III"; let integer = roman_to_int(roman); println!("{}", integer); }
Attempts:
2 left
💡 Hint
Repeated 'I's add up their values.
✗ Incorrect
Each 'I' is 1, so three 'I's sum to 3.
🧠 Conceptual
advancedUnderstanding Subtraction Rule in Roman Numerals
In the Roman to Integer conversion algorithm, why do we subtract the value of a Roman numeral character when it is less than the previous character processed?
Attempts:
2 left
💡 Hint
Think about how Roman numerals like IV or IX represent numbers.
✗ Incorrect
In Roman numerals, placing a smaller numeral before a larger one means subtracting the smaller from the larger (e.g., IV = 4). The algorithm uses this rule by subtracting when the current value is less than the previous.
🔧 Debug
advancedIdentify the Error in Roman to Integer Conversion Code
What error will occur if the following Rust code is run to convert Roman numerals?
DSA Rust
fn roman_to_int(s: &str) -> i32 { let mut result = 0; let mut prev = 0; for c in s.chars() { let value = match c { 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000, _ => 0, }; if value < prev { result -= value; } else { result += value; } prev = value; } result } fn main() { let roman = "IX"; let integer = roman_to_int(roman); println!("{}", integer); }
Attempts:
2 left
💡 Hint
Check the direction of iteration and how subtraction is applied.
✗ Incorrect
The algorithm must process characters from right to left to correctly apply the subtraction rule. Processing left to right causes wrong results.
🚀 Application
expertCalculate the Integer Value of Complex Roman Numeral
Using the Roman to Integer conversion logic, what is the integer value of the Roman numeral
"MMMDCCCLXXXVIII"?Attempts:
2 left
💡 Hint
Break down the numeral into thousands, hundreds, tens, and ones.
✗ Incorrect
MMMDCCCLXXXVIII = 3000 + 800 + 80 + 8 = 3888.
