Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.