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 TypeScript code that converts a Roman numeral to an integer?
DSA Typescript
function romanToInt(s: string): number {
const map: {[key: string]: number} = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
};
let total = 0;
for (let i = 0; i < s.length; i++) {
const current = map[s[i]];
const next = map[s[i + 1]] || 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}
console.log(romanToInt('MCMXCIV'));Attempts:
2 left
💡 Hint
Remember that when a smaller numeral appears before a larger one, you subtract it.
✗ Incorrect
The Roman numeral 'MCMXCIV' breaks down as M(1000) + CM(900) + XC(90) + IV(4) = 1994.
🧠 Conceptual
intermediateUnderstanding Subtraction Rule in Roman Numerals
In Roman numeral conversion, why do we subtract the value of a smaller numeral when it appears before a larger numeral?
Attempts:
2 left
💡 Hint
Think about how 'IV' means 4, not 6.
✗ Incorrect
When a smaller numeral comes before a larger one, it means subtract the smaller from the larger, e.g., IV = 5 - 1 = 4.
🔧 Debug
advancedIdentify the Error in Roman to Integer Conversion Code
What error will occur when running this TypeScript code snippet?
DSA Typescript
function romanToInt(s: string): number {
const map: {[key: string]: number} = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
};
let total = 0;
for (let i = 0; i <= s.length; i++) {
const current = map[s[i]];
const next = map[s[i + 1]] || 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}
console.log(romanToInt('IX'));Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs until i <= s.length, so s[i] is undefined on the last iteration, map[s[i]] returns undefined, and total += undefined results in NaN.
📝 Syntax
advancedWhich Option Causes a Syntax Error?
Which of the following TypeScript code snippets for Roman to Integer conversion will cause a syntax error?
Attempts:
2 left
💡 Hint
Look carefully at the missing semicolon or braces.
✗ Incorrect
Option A is missing a semicolon at the end of the statement, which causes a syntax error in TypeScript strict mode.
🚀 Application
expertResult of Complex Roman Numeral Conversion
What is the integer value returned by the following TypeScript function call?
DSA Typescript
function romanToInt(s: string): number {
const map: {[key: string]: number} = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
};
let total = 0;
for (let i = 0; i < s.length; i++) {
const current = map[s[i]];
const next = map[s[i + 1]] || 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}
console.log(romanToInt('MMMDCCCLXXXVIII'));Attempts:
2 left
💡 Hint
Break down the numeral into thousands, hundreds, tens, and ones.
✗ Incorrect
'MMMDCCCLXXXVIII' is 3000 (MMM) + 800 (DCCC) + 80 (LXXX) + 8 (VIII) = 3888.
