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 C++ code that converts a Roman numeral string to an integer?
DSA C++
int romanToInt(string s) { unordered_map<char, int> values{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; int total = 0; for (int i = 0; i < s.size(); i++) { if (i + 1 < s.size() && values[s[i]] < values[s[i+1]]) { total -= values[s[i]]; } else { total += values[s[i]]; } } return total; } int main() { cout << romanToInt("MCMXCIV") << endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that when a smaller value precedes a larger one, you subtract it.
✗ Incorrect
The Roman numeral MCMXCIV is 1000 + (1000-100) + (100-10) + (5-1) = 1994.
❓ Predict Output
intermediateResult of Roman to Integer Conversion with Repeated Numerals
What is the output of this code when converting the Roman numeral "III" to an integer?
DSA C++
int romanToInt(string s) { unordered_map<char, int> values{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; int total = 0; for (int i = 0; i < s.size(); i++) { if (i + 1 < s.size() && values[s[i]] < values[s[i+1]]) { total -= values[s[i]]; } else { total += values[s[i]]; } } return total; } int main() { cout << romanToInt("III") << endl; return 0; }
Attempts:
2 left
💡 Hint
Repeated 'I's add up.
✗ Incorrect
Each 'I' is 1, so three 'I's add up to 3.
❓ Predict Output
advancedOutput for Complex Roman Numeral with Subtractions
What is the output of this code when converting the Roman numeral "XLII" to an integer?
DSA C++
int romanToInt(string s) { unordered_map<char, int> values{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; int total = 0; for (int i = 0; i < s.size(); i++) { if (i + 1 < s.size() && values[s[i]] < values[s[i+1]]) { total -= values[s[i]]; } else { total += values[s[i]]; } } return total; } int main() { cout << romanToInt("XLII") << endl; return 0; }
Attempts:
2 left
💡 Hint
XL means 50 - 10.
✗ Incorrect
XL is 40 (50 - 10), plus II is 2, total 42.
🧠 Conceptual
advancedWhy Subtraction is Needed in Roman to Integer Conversion
Why does the Roman to Integer conversion algorithm subtract the value of a numeral when it appears before a larger numeral?
Attempts:
2 left
💡 Hint
Think about how Roman numerals represent numbers like 4 and 9.
✗ Incorrect
Roman numerals place smaller values before larger ones to indicate subtraction, e.g., IV = 5 - 1 = 4.
❓ Predict Output
expertOutput of Roman to Integer Conversion for Large Numeral
What is the output of this code when converting the Roman numeral "MMMDCCCLXXXVIII" to an integer?
DSA C++
int romanToInt(string s) { unordered_map<char, int> values{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; int total = 0; for (int i = 0; i < s.size(); i++) { if (i + 1 < s.size() && values[s[i]] < values[s[i+1]]) { total -= values[s[i]]; } else { total += values[s[i]]; } } return total; } int main() { cout << romanToInt("MMMDCCCLXXXVIII") << endl; return 0; }
Attempts:
2 left
💡 Hint
Add all values since no smaller numeral precedes a larger one.
✗ Incorrect
MMMDCCCLXXXVIII is 3000 + 800 + 80 + 8 = 3888.
