Learning Machine
Definitely one of the cleverest solutions I have ever come across

Are you a software engineer or data scientist? Did you ever prepare yourself for Coding interviews? If you answer yes for both questions, chances are you have heard of LeetCode.
For those unfamiliar with LeetCode, it is a site filled with thousands of coding problems that people can use to train their coding skills. By the time of writing, it has 1835 problems and still growing.
I highly recommend everyone to keep practicing their problem-solving skills regardless of your specialty. In my experience, almost all positions ranging from back-end, front-end, machine learning engineer, to data scientist usually have a coding round or more in the interview process.
If you go to the problems page, you would see the problems listed inside a table, just like this.

Most problem comes with a solution provided by LeetCode, but some of them require you to subscribe to LeetCode for $35/month or $159/year. I’m just saying that people will almost always post their own solution in the discussion page, so do what you will with that information.
Seriously though, I often found the best solutions in the discussion page rather than the solution page.
Here’s one of my favorite solutions that I come across in LeetCode.
Roman To Integer Problem
One problem listed in the first page is the Roman to Integer problem, where the task is to convert roman numerals to integer. For example, "VIII" becomes 8 and "MCMXCVII" becomes 1997.
Luckily, the roman numeral is limited within the range 1–3999, so the biggest numeral that we will use is M which represents 1000.
The hardest part of the problem is handling cases like "IV", "IX", "XL", etc. where a smaller number precedes the larger one and causes the number to be subtracted instead of added.
So there I was, trying to code the solution for 10 minutes before getting it to work and pass all the test cases.
As always, I checked the discussion page to know other people’s approach towards the problem. Then, I came across this solution that claims it beats 99.78%. Of course I was curious and had to click it. Here’s the solution.
The Romans would most likely be angered by how it butchers their numeric system. Sorry guys. –hgrsd
And I would agree with him 100%.
Instead of creating special logic for "IV", "IX", and so on, this solution simply replaces them with an easier to process form of "IIII", "VIIII", and so on.
This would make the problem much easier because the code simply needs to add every single character inside the roman numeral.
I was taken aback by how ingenious the solution is. True, it violates the rule of proper roman numeral, but it can produce correct answer given the constraints of the problem.
Here’s the solution that I came up with in comparison.
In principle, it checks if the next roman character is bigger than the current one. If yes, then it would instead subtract that amount instead of adding it.
Given the constraints of the problem, both solutions will work fine. However, the first solution gave me a hearty chuckle while my solution is a bit boring in comparison.
Conclusions
It fascinates me to see how different other people’s approaches are from mine while facing the same problem. I want to say thank you to LeetCode for providing the solution and discussion page for each problem.
I always found the best and most creative solution inside the discussion page.
I lost count of how many times I said to myself, "Why didn’t I think of that earlier?" after seeing out-of-the-box solutions such as this one.
A lot of the solutions for other problems are also as unconventional, but this one easily takes the cake.
Even if you are not preparing for interviews, I would highly recommend you to practice LeetCode once in a while. It will help you maintain your coding skill and stay on top of your game.
Who knows what you might come up with or find in the problems?
Learning Machine is a series of stories about things that I have learned and deemed interesting enough to share. Sometimes it will be about the fundamentals of Machine Learning too. Get regular updates on new stories and become a Medium member to read unlimited stories.