avatarJennifer Fu

Summary

This context provides a comprehensive guide on converting Roman numerals to numbers and vice versa using JavaScript, along with an explanation of Roman numerals and their usage in coding interviews.

Abstract

The context begins with an introduction to Roman numerals, their history, and their usage in modern times. It then moves on to the main topic of converting Roman numerals to numbers using JavaScript. The author provides two algorithms for this conversion, one with a reducer and one without. Both algorithms have a time complexity of O(n) and a space complexity of O(1). The context also covers converting numbers to Roman numerals using a similar approach. The author then demonstrates how to build a calculator using Roman numerals, first with normal digits and then with Roman numerals. The algorithms used for this calculator also have a time complexity of O(n) and a space complexity of O(1). The context concludes with a note on the usage of Roman numerals in coding interviews and provides links to other interview guides.

Bullet points

  • Roman numerals are a numeral system originating from ancient Rome, still used today in some occasions.
  • The author provides two algorithms for converting Roman numerals to numbers using JavaScript, one with a reducer and one without.
  • Both algorithms for converting Roman numerals to numbers have a time complexity of O(n) and a space complexity of O(1).
  • The context also covers converting numbers to Roman numerals using a similar approach.
  • The author demonstrates how to build a calculator using Roman numerals, first with normal digits and then with Roman numerals.
  • The algorithms used for the calculator also have a time complexity of O(n) and a space complexity of O(1).
  • The context concludes with a note on the usage of Roman numerals in coding interviews and provides links to other interview guides.

JavaScript Interview Question: Convert Roman Numerals To Numbers

A JavaScript guide to Roman numerals for interview prep and daily coding

Image credit: Author

Roman numerals are a numeral system that originated from ancient Rome. It remains used today in some occasions. Modern usage of Roman numerals employs seven symbols, each is assigned an integer value:

  • I: 1
  • V: 5
  • X: 10
  • L: 50
  • C: 100
  • D: 500
  • M: 1000

A Roman numeral is composed with upper cases of MDCLXVI. It is written with the largest symbol at the left and the smallest symbol at right, consist with Arabic numerals. For example, MDCLXVI stands for 1666 (1000 + 500 + 100 + 50 + 10 + 5 + 1). A symbol can be repeated, such as III (3). Generally speaking, all symbols are in addictive notations. However, when a lesser symbol is at the left, it defines a subtractive notation. These are all subtractive notations:

  • IV (5 — 1 = 4)
  • IX (10 – 1 = 9),
  • XL ( 50 — 10 = 40)
  • XC (100 - 10 = 90)
  • CD (500 — 100 = 400)
  • CM (1000 — 100 = 900)

Sometimes, Roman numerals appear in a real interview. It is important to master the principle and pass the interview questions.

Convert a Roman Numeral to a Number

We have been asked to write an algorithm to convert a Roman numeral to a number.

If there are no subtractive notations, it is straightforward. Simply convert every Roman numeral to a number and sum them up.

If there are subtractive notations, we may accidentally add the left-side symbols wrongly. Also, the left-side symbols mean subtraction. Therefore, the solution is to subtract the left-side symbols twice when it is detected.

Here is the algorithm to convert a Roman numeral to a number:

Examine the code from line 18 to line 40. Does it look like a JavaScript reducer?

Yes, indeed. We can adopt Array APIs, such as from, map, and reduce to make the code really condensed.

Both algorithms’ time complexity is O(n), and space complexity is O(1).

These are verifying tests, which work for both gists:

Convert a Number to a Roman Numeral

After writing an algorithm to convert a Roman numeral to a number, we are going to do the opposite: convert a number to a Roman numeral.

Create a new convertor, from the largest symbol to the smallest symbol, including 9’s and 4’s.

  • M: 1000
  • CM: 900
  • D: 500
  • CD: 400
  • C: 100
  • XC: 90
  • L: 50
  • XL: 40
  • X: 10
  • IX: 9
  • V: 5
  • IV: 4
  • I: 1

If a number is larger than or equals to the current symbol, take the symbol and subtract the number. Repeat the process until the remaining value is 0.

We use 8 as an example:

  • 8 is smaller than any symbol before 5.
  • 8 is larger than 5. Add V to the result, and the remaining number is 3.
  • 3 is smaller than 5 and smaller than 4.
  • 3 is larger than 1. Add I to the result to become VI, and the remaining number is 2.
  • 2 is larger than 1. Add I to the result to become VII, and the remaining number is 1.
  • 2 equals to 1. Add I to the result to become VIII, and the remaining number is 0.

Here is the algorithm:

Look at the above code. Do you see anything to improve?

For the loop at lines 31 - 38, one number is subtracted each time. For the number of 5000, it needs to subtract 1000 for 5 times. This could be slow when a number is large. Since 5000 / 1000 is 5, it is predictable that 5 M’s are needed. When the number is not divisible, a floor can be taken. Meanwhile, the remainder is the remaining number.

Here is the improved algorithm:

At line 36, we adopt the String API, repeat, which is mentioned in string article.

Both algorithms’ time complexity is O(n), and space complexity is O(1).

These are verifying tests, which work for both gists:

Roman Numeral Calculator

We converted Roman numerals to a numbers, and vice versa. Roman numerals no longer look foreign to us. They can be applied to various number problems.

Let’s build a calculator using Roman numerals. This calculator accepts Roman numerals, spaces, parentheses, plus, and minus. For example, typing "I + II " will yield number III.

First, we need to build a calculator using normal digits, which can calculate " 1 + 2 " to get number 3.

The algorithm’s time complexity is O(n), and space complexity is O(1).

These are verifying tests:

To build a calculator using Roman numerals, we need to include the previous function to convert a Roman numeral to a number (lines 25 - 40 in below code). And then replace the normal digits related block with Roman numerals related block (lines 71 - 79).

We also need to include the previous function to convert a number to a Roman numeral(lines 42 - 57), and call it before return the value at line 106.

Totally, there are 4-line differences (Lines 72, 75, 76, and 106).

In the below code, we share the complex converter for both conversions.

The algorithm’s time complexity is O(n), and space complexity is O(1).

These are verifying tests:

Interestingly, the line 3 has the output of 0, and it is converted to Roman numeral as an empty string. The number 0 did not originally have its own Roman numeral, but the word nulla (a Latin word meaning “none”) was used by medieval scholars to represent 0.

Conclusion

Facebook, Microsoft, and other companies have given interview questions on Roman numerals. Practice makes perfect.

The following are other interview guides:

Thanks for reading.

Want to Connect?

If you are interested, check out my directory of web development articles.
JavaScript
Web Development
Software Engineering
Programming
Algorithms
Recommended from ReadMedium