Implement Translate Roman Numerals in JavaScript
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Implement Translate Roman Numerals in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
This is the third article in a series about programming interview questions and how to think through and solve them! This article will cover the roman numeral translation programming interview question in JavaScript.
Given a roman numeral as input, write a function that converts the roman numeral to a number and outputs it.* When a smaller numeral appears before a larger one, it becomes a subtractive operation. You can assume only one smaller numeral may appear in front of larger one. For example: if given the input VI, the output should be 6 (5 + 1 = 6) or if given the input IV, the output should be 4 (5–1 = 4).*
The Problem
The problem of implementing Roman numeral translation in JavaScript involves converting Roman numeral representations into their corresponding numeric values. To solve this problem, you need to design an algorithm that can interpret the Roman numeral symbols and calculate the decimal equivalent. The algorithm should handle various Roman numeral rules, such as additive and subtractive combinations, where certain symbols represent the sum or subtraction of values. By breaking down the problem, you can identify key steps, such as iterating through the Roman numeral string, comparing symbols, and calculating the final numeric value. JavaScript provides powerful string manipulation capabilities and conditional statements that can be utilized to implement an efficient and accurate translation function. The solution to this problem allows for seamless conversion between Roman numerals and decimal numbers, enabling applications to work with Roman numeral inputs and perform numerical operations with ease.
Analysis
One of our biggest challenges here is knowing if we should be adding the current letter within the roman numeral or subtract it. I decided that, while iterating through, I could peak to the right of the current letter and decide whether to add or subtract. This route would give me a linear time complexity at worst case. Let’s take a look at the pseudocode.
Pseudocode
Split input at each character
Iterate through split input Save current letter and next letter into variables If current letter is undefined Return 'null' else if current letter is less than next letter Add current letter minus next letter to result var Double increment count to skip next letter else Add current letter to result Return result
Time Complexity
The time complexity of the above algorithm should be the same as the number of items produced. This is because as the list grows larger, we have to continue to check each letter to add the value. Therefore, with an input size of n the time complexity is linear — O(n).
Code
Nice job making it through this one! As you work through more of the advanced challenges, you’ll need to start utilizing data structures. Interested in learning some now? I wrote a series on data structures in JavaScript here.
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
I founded Higglo Digital and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a beautifully designed website, check us out.
I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!