Bird
Raised Fist0
DSA Rustprogramming~20 mins

Roman to Integer Conversion in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
}
A1904
B1994
C1094
D1949
Attempts:
2 left
💡 Hint
Remember that in Roman numerals, smaller values before larger ones mean subtraction.
❓ Predict Output
intermediate
2:00remaining
Output 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);
}
A3
B1
C2
D4
Attempts:
2 left
💡 Hint
Repeated 'I's add up their values.
🧠 Conceptual
advanced
1:30remaining
Understanding 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?
ABecause all Roman numerals less than 10 are subtracted by default.
BBecause the algorithm processes characters from left to right and needs to correct the sum.
CBecause smaller numerals before larger ones indicate subtraction in Roman numerals.
DBecause the previous character is always added twice and needs adjustment.
Attempts:
2 left
💡 Hint
Think about how Roman numerals like IV or IX represent numbers.
🔧 Debug
advanced
2:00remaining
Identify 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);
}
AThe output will be incorrect because the code processes characters left to right instead of right to left.
BThe code will panic at runtime due to invalid character handling.
CThe code will produce a syntax error due to missing semicolons.
DThe code will correctly output 9.
Attempts:
2 left
💡 Hint
Check the direction of iteration and how subtraction is applied.
🚀 Application
expert
3:00remaining
Calculate the Integer Value of Complex Roman Numeral
Using the Roman to Integer conversion logic, what is the integer value of the Roman numeral "MMMDCCCLXXXVIII"?
A3889
B3890
C3887
D3888
Attempts:
2 left
💡 Hint
Break down the numeral into thousands, hundreds, tens, and ones.

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

  1. Step 1: Understand Roman numeral rules

    Roman numerals are added normally, but if a smaller numeral appears before a larger one, it is subtracted.
  2. 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.
  3. Final Answer:

    Add values of symbols, subtract if a smaller symbol is before a larger one -> Option B
  4. 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

  1. Step 1: Check Rust function syntax

    Rust functions start with fn, use snake_case, and specify parameter types and return type correctly.
  2. 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.
  3. Final Answer:

    fn roman_to_int(s: &str) -> i32 { -> Option A
  4. 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

  1. Step 1: Trace characters in reverse

    Input "IX" reversed is 'X' then 'I'. 'X' = 10, 'I' = 1.
  2. Step 2: Calculate total with subtraction rule

    Start total=0, prev=0. 'X' (10) >= prev(0), total=10, prev=10. Next 'I' (1) < prev(10), total=10-1=9, prev=1.
  3. Final Answer:

    9 -> Option D
  4. Quick Check:

    "IX" = 9 [OK]
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

  1. 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.
  2. 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.
  3. Final Answer:

    Match arms missing some Roman numerals -> Option C
  4. 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
}
hard
A. 1994
B. 1904
C. 2004
D. None of the above

Solution

  1. Step 1: Reverse and assign values

    Reverse "MCMXCIV" to "VICXMCM". Values: V=5, I=1, C=100, X=10, M=1000, C=100, M=1000.
  2. Step 2: Calculate total with subtraction rule

    Start total=0, prev=0. - V(5) >= prev(0): total=5, prev=5 - I(1) < prev(5): total=5-1=4, prev=1 - C(100) >= prev(1): total=4+100=104, prev=100 - X(10) < prev(100): total=104-10=94, prev=10 - M(1000) >= prev(10): total=94+1000=1094, prev=1000 - C(100) < prev(1000): total=1094-100=994, prev=100 - M(1000) >= prev(100): total=994+1000=1994, prev=1000
  3. Final Answer:

    1994 -> Option A
  4. Quick Check:

    "MCMXCIV" = 1994 [OK]
Hint: Reverse string, subtract if smaller before larger [OK]
Common Mistakes:
  • Not reversing string before processing
  • Misassigning values to symbols
  • Ignoring subtraction rule
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard