In the Roman to Integer algorithm, why do we iterate from left to right?
ABecause Roman numerals are read left to right
BTo easily compare current and next numeral
CTo reverse the string
DTo sort the numerals
✗ Incorrect
Iterating left to right lets us compare current and next numeral to decide add or subtract.
Explain the subtraction rule in Roman to Integer conversion with an example.
Think about why 'IV' is 4, not 6.
You got /3 concepts.
Describe how you would implement Roman to Integer conversion in Rust using a HashMap.
Focus on data structure and iteration logic.
You got /4 concepts.
Practice
(1/5)
1. What is the main rule to convert Roman numerals to integers?
easy
A. Multiply values of all symbols
B. Add values of symbols, subtract if a smaller symbol is before a larger one
C. Add values only if symbols are the same
D. Subtract values of all symbols
Solution
Step 1: Understand Roman numeral rules
Roman numerals are added normally, but if a smaller numeral appears before a larger one, it is subtracted.
Step 2: Apply the rule to conversion
When converting, add each symbol's value unless a smaller symbol precedes a larger one, then subtract the smaller.
Final Answer:
Add values of symbols, subtract if a smaller symbol is before a larger one -> Option B
Quick Check:
Roman to integer = add or subtract based on order [OK]
Hint: Subtract when smaller numeral precedes larger one [OK]
Common Mistakes:
Always adding without subtraction
Multiplying values instead of adding
Ignoring order of symbols
2. Which Rust code snippet correctly defines a function header for converting a Roman numeral string to an integer?
easy
A. fn roman_to_int(s: &str) -> i32 {
B. fn romanToInt(s: String) -> int {
C. function roman_to_int(s: &str) -> i32 {
D. fn roman_to_int(s: &String) -> i64 {
Solution
Step 1: Check Rust function syntax
Rust functions start with fn, use snake_case, and specify parameter types and return type correctly.
Step 2: Validate each option
fn roman_to_int(s: &str) -> i32 { uses correct Rust syntax: fn, snake_case, &str parameter, and i32 return type. Others have syntax errors or wrong types.
Final Answer:
fn roman_to_int(s: &str) -> i32 { -> Option A
Quick Check:
Rust fn header with &str and i32 return [OK]
Hint: Use fn, snake_case, &str param, i32 return [OK]
Common Mistakes:
Using camelCase instead of snake_case
Wrong parameter or return types
Using 'function' keyword (not Rust)
3. What is the output of this Rust code snippet?
fn main() {
let s = "IX";
let result = roman_to_int(s);
println!("{}", result);
}
fn roman_to_int(s: &str) -> i32 {
let mut total = 0;
let mut prev = 0;
for c in s.chars().rev() {
let value = match c {
'I' => 1,
'V' => 5,
'X' => 10,
_ => 0,
};
if value < prev {
total -= value;
} else {
total += value;
}
prev = value;
}
total
}
medium
A. 6
B. 11
C. Error
D. 9
Solution
Step 1: Trace characters in reverse
Input "IX" reversed is 'X' then 'I'. 'X' = 10, 'I' = 1.
Hint: Traverse string backward, subtract if smaller before larger [OK]
Common Mistakes:
Not reversing string for correct subtraction
Adding all values without subtraction
Misassigning values to characters
4. Identify the bug in this Rust function for Roman to integer conversion:
fn roman_to_int(s: &str) -> i32 {
let mut total = 0;
let mut prev = 0;
for c in s.chars() {
let value = match c {
'I' => 1,
'V' => 5,
'X' => 10,
_ => 0,
};
if value > prev {
total += value - 2 * prev;
} else {
total += value;
}
prev = value;
}
total
}
medium
A. Loop should iterate in reverse order
B. Subtracting twice prev is incorrect
C. Match arms missing some Roman numerals
D. prev variable should be mutable
Solution
Step 1: Inspect the match expression
The match only handles 'I'(1), 'V'(5), 'X'(10). It misses 'L'(50), 'C'(100), 'D'(500), 'M'(1000), causing other symbols to map to 0 and produce wrong results.
Step 2: Verify other options
A: Forward iteration with 'value - 2*prev' is valid for handling subtractive notation. B: The 2*prev subtraction corrects the previously added prev value. D: 'prev' is already mutable.
Final Answer:
Match arms missing some Roman numerals -> Option C
Quick Check:
Match must cover I,V,X,L,C,D,M [OK]
Hint: Include match arms for all Roman numerals [OK]
Common Mistakes:
Incomplete match arms
Not handling all Roman numerals
Forgetting to make prev mutable (already mutable here)
5. Given the Roman numeral string "MCMXCIV", what integer does the following Rust function return?
fn roman_to_int(s: &str) -> i32 {
let mut total = 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 {
total -= value;
} else {
total += value;
}
prev = value;
}
total
}