Bird
Raised Fist0
DSA Cprogramming~20 mins

Roman to Integer Conversion in DSA C - 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 C code when the input Roman numeral is "MCMXCIV"?
DSA C
int romanToInt(char * s) {
    int map[26] = {0};
    map['I' - 'A'] = 1;
    map['V' - 'A'] = 5;
    map['X' - 'A'] = 10;
    map['L' - 'A'] = 50;
    map['C' - 'A'] = 100;
    map['D' - 'A'] = 500;
    map['M' - 'A'] = 1000;

    int total = 0;
    int i = 0;
    while (s[i]) {
        int value = map[s[i] - 'A'];
        int nextValue = map[s[i+1] - 'A'];
        if (nextValue > value) {
            total += (nextValue - value);
            i += 2;
        } else {
            total += value;
            i++;
        }
    }
    return total;
}

int main() {
    char roman[] = "MCMXCIV";
    int result = romanToInt(roman);
    printf("%d\n", result);
    return 0;
}
A1994
B1094
C1949
DNone of the above
Attempts:
2 left
💡 Hint
Remember that when a smaller value precedes a larger value, you subtract the smaller from the larger.
❓ Predict Output
intermediate
2:00remaining
Output of Roman to Integer Conversion with Repeated Numerals
What is the output of the following C code when the input Roman numeral is "III"?
DSA C
int romanToInt(char * s) {
    int map[26] = {0};
    map['I' - 'A'] = 1;
    map['V' - 'A'] = 5;
    map['X' - 'A'] = 10;
    map['L' - 'A'] = 50;
    map['C' - 'A'] = 100;
    map['D' - 'A'] = 500;
    map['M' - 'A'] = 1000;

    int total = 0;
    int i = 0;
    while (s[i]) {
        int value = map[s[i] - 'A'];
        int nextValue = map[s[i+1] - 'A'];
        if (nextValue > value) {
            total += (nextValue - value);
            i += 2;
        } else {
            total += value;
            i++;
        }
    }
    return total;
}

int main() {
    char roman[] = "III";
    int result = romanToInt(roman);
    printf("%d\n", result);
    return 0;
}
A6
B3
C1
D0
Attempts:
2 left
💡 Hint
Repeated numerals add their values.
🔧 Debug
advanced
2:00remaining
Identify the Error in Roman to Integer Conversion Code
What error will the following C code produce when run with input "VI"?
DSA C
int romanToInt(char * s) {
    int map[26] = {0};
    map['I' - 'A'] = 1;
    map['V' - 'A'] = 5;
    map['X' - 'A'] = 10;
    map['L' - 'A'] = 50;
    map['C' - 'A'] = 100;
    map['D' - 'A'] = 500;
    map['M' - 'A'] = 1000;

    int total = 0;
    int i = 0;
    while (s[i]) {
        int value = map[s[i] - 'A'];
        int nextValue = map[s[i+1] - 'A'];
        if (nextValue > value) {
            total += (nextValue - value);
            i += 2;
        } else {
            total += value;
            i++;
        }
    }
    return total;
}

int main() {
    char roman[] = "VI";
    int result = romanToInt(roman);
    printf("%d\n", result);
    return 0;
}
ASegmentation fault due to accessing s[i+1] when i+1 is out of bounds
BPrints 4 correctly
CPrints 6 incorrectly
DCompilation error due to missing header
Attempts:
2 left
💡 Hint
Check how the code accesses s[i+1] without verifying if it exists.
❓ Predict Output
advanced
2:00remaining
Output of Roman to Integer Conversion with Mixed Numerals
What is the output of the following C code when the input Roman numeral is "XLII"?
DSA C
int romanToInt(char * s) {
    int map[26] = {0};
    map['I' - 'A'] = 1;
    map['V' - 'A'] = 5;
    map['X' - 'A'] = 10;
    map['L' - 'A'] = 50;
    map['C' - 'A'] = 100;
    map['D' - 'A'] = 500;
    map['M' - 'A'] = 1000;

    int total = 0;
    int i = 0;
    while (s[i]) {
        int value = map[s[i] - 'A'];
        int nextValue = map[s[i+1] - 'A'];
        if (nextValue > value) {
            total += (nextValue - value);
            i += 2;
        } else {
            total += value;
            i++;
        }
    }
    return total;
}

int main() {
    char roman[] = "XLII";
    int result = romanToInt(roman);
    printf("%d\n", result);
    return 0;
}
A44
B52
C40
D42
Attempts:
2 left
💡 Hint
XL is 40, II is 2, sum them.
🧠 Conceptual
expert
2:00remaining
Why does the Roman to Integer Conversion Algorithm Check Next Character?
Why does the Roman to Integer conversion algorithm check the next character's value when processing the current character?
ATo skip invalid characters in the input string
BTo convert the Roman numeral to lowercase before processing
CTo decide whether to add or subtract the current numeral's value based on Roman numeral rules
DTo count the total number of characters in the string
Attempts:
2 left
💡 Hint
Roman numerals use subtractive notation when a smaller numeral precedes a larger one.

Practice

(1/5)
1. What is the main rule to convert Roman numerals to integers?
Example: IV = 4, VI = 6
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 in alphabetical order
D. Subtract values of all symbols

Solution

  1. Step 1: Understand Roman numeral rules

    Roman numerals add values of symbols, but if a smaller symbol appears before a larger one, it is subtracted.
  2. Step 2: Apply rule to example

    For IV, I (1) is before V (5), so subtract 1 from 5 = 4. For VI, V (5) before I (1), so add 5 + 1 = 6.
  3. Final Answer:

    Add values of symbols, subtract if a smaller symbol is before a larger one -> Option B
  4. Quick Check:

    IV = 4, VI = 6 [OK]
Hint: Subtract when smaller symbol precedes larger one [OK]
Common Mistakes:
  • Always adding without subtraction
  • Multiplying symbols instead of adding
  • Ignoring order of symbols
2. Which of the following C code snippets correctly initializes a map for Roman numeral values?
easy
A. int values = {'I':1, 'V':5, 'X':10};
B. int values[] = {I=1, V=5, X=10};
C. int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10};
D. int values[3] = {1, 5, 10};

Solution

  1. Step 1: Understand C array initialization for character keys

    In C, to map characters to values, an array indexed by character offset is used, e.g., values['I'-'A'] = 1.
  2. Step 2: Check each option

    int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10}; uses correct syntax for sparse initialization. Options B and C use invalid syntax for C. int values[3] = {1, 5, 10}; initializes array without mapping to characters.
  3. Final Answer:

    int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10}; -> Option C
  4. Quick Check:

    Correct C syntax for char-indexed array [OK]
Hint: Use array with char offset for mapping in C [OK]
Common Mistakes:
  • Using dictionary syntax not valid in C
  • Not indexing array by character offset
  • Initializing array without specifying indices
3. What is the output of this C code snippet?
char *s = "MCMXCIV";
int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50, ['C'-'A']=100, ['D'-'A']=500, ['M'-'A']=1000};
int result = 0, i = 0;
while (s[i]) {
  int curr = values[s[i]-'A'];
  int next = values[s[i+1]-'A'];
  if (next > curr) result -= curr;
  else result += curr;
  i++;
}
printf("%d", result);
medium
A. Error
B. 1904
C. 1494
D. 1994

Solution

  1. Step 1: Trace each character and apply addition/subtraction

    String: M(1000), C(100), M(1000), X(10), C(100), I(1), V(5). Check pairs:
    M(1000) next C(100) -> 100 < 1000? No, add 1000
    C(100) next M(1000) -> 1000 > 100, subtract 100
    M(1000) next X(10) -> 10 < 1000, add 1000
    X(10) next C(100) -> 100 > 10, subtract 10
    C(100) next I(1) -> 1 < 100, add 100
    I(1) next V(5) -> 5 > 1, subtract 1
    V(5) next 0, add 5
  2. Step 2: Calculate total

    1000 - 100 + 1000 - 10 + 100 - 1 + 5 = 1994
  3. Final Answer:

    1994 -> Option D
  4. Quick Check:

    MCMXCIV = 1994 [OK]
Hint: Subtract when next symbol is larger [OK]
Common Mistakes:
  • Adding all values without subtraction
  • Off-by-one error accessing next character
  • Incorrect mapping of Roman symbols
4. Find the bug in this C code for Roman to Integer conversion:
int romanToInt(char *s) {
  int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50, ['C'-'A']=100, ['D'-'A']=500, ['M'-'A']=1000};
  int result = 0, i = 0;
  while (s[i]) {
    int curr = values[s[i]-'A'];
    int next = values[s[i+1]-'A'];
    if (curr < next) result += curr;
    else result -= curr;
    i++;
  }
  return result;
}
medium
A. The addition and subtraction logic is reversed
B. Array indexing causes out-of-bounds error
C. Values array is not initialized correctly
D. Function does not handle empty string

Solution

  1. Step 1: Analyze addition and subtraction conditions

    Code adds current value if curr < next, else subtracts. This is opposite of Roman rules where smaller before larger means subtract.
  2. Step 2: Correct logic

    It should subtract curr if curr < next, else add curr.
  3. Final Answer:

    The addition and subtraction logic is reversed -> Option A
  4. Quick Check:

    Subtract when smaller before larger [OK]
Hint: Subtract if current < next, else add [OK]
Common Mistakes:
  • Swapping add and subtract conditions
  • Ignoring last character's next value
  • Not initializing values array properly
5. Given the Roman numeral string "XLIX", which of the following C functions correctly returns its integer value?
hard
A. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr
B. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr>next) res-=curr; else res+=curr; i++; } return res; }
C. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A']; res+=curr; i++; } return res; }
D. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr<=next) res-=curr; else res+=curr; i++; } return res; }

Solution

  1. Step 1: Check logic for subtraction and addition

    int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr
  2. Step 2: Verify with example "XLIX"

    X(10) < L(50) subtract 10
    L(50) > I(1) add 50
    I(1) < X(10) subtract 1
    X(10) no next add 10
    Total = -10 + 50 - 1 + 10 = 49
  3. Final Answer:

    Option A correctly returns 49 for "XLIX" -> Option A
  4. Quick Check:

    Subtract if smaller before larger [OK]
Hint: Subtract if current < next, else add [OK]
Common Mistakes:
  • Using <= instead of < for subtraction
  • Adding all values without subtraction
  • Incorrect comparison operators
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