A coding problem so easy yet so difficult

Amit Amola
4 min readDec 11, 2020

--

So I recently joined medium and also started to do some coding again. Now here’s something you don’t get to see every day- a problem which looked so simple yet was so difficult that I had to eventually google for the solution.

Let me come straight to the point, this is that problem that I came across in Hackerrank:

The Problem Statement

I straight away started to solve it and it took me or it would take anyone seconds to come with a simple solution.

It was wrong!

Because this question had a lot of conditions:

  1. Using only arithmetic operations, a single for loop, and a single print statement?
  2. Use no more than two lines.
  3. Using anything related to strings will give a score of 0. So you can’t use strings either.

So, can you do it now?

Took me straight 15 to 20 minutes and a lot of head-scratching but I still couldn’t think how to solve this.

So I opened google and looked into the discussion of this problem within Hackerrank. And as soon as I saw the answer, I had that familiar feeling that we all have in such cases- “Hey, I could’ve thought of this, why couldn’t I?” Indeed, why couldn’t I?

Before I share the solution as well, please try it yourself and feel free to share if you were able to think of this solution right away too or did it take some time or you had to see the answer.

The solution:

Isn’t it just so subtle yet genius? All we had to do was get the nearest power of 10 to the amount of number and then take integer division using 9 which will always end up giving 11…1 and then multiply this with the number.

So for example, for 5, it’s like:

Step 1: 10**5 = 100000
Step 2: 100000//9 = 11111
Step 3: 11111*5 = 55555

And that’s it. So for each value of i, we get this pattern.

The bigger question

The main reason to write this post isn’t the question and answer itself. There was this post by a fellow member who I am quoting now:

“I’m glad to have learned something new today. However, that “something new” was more maths than programming.

IMO, this problem forces artificial restrictions so as to leave you with only one solution. And if you enjoy recreational mathematics, then you would know it right away, but otherwise you’re going to be stuck for a while.

In this respect, it doesn’t help to improve programming skill, IMHO. And especially not Python skill (this is an introductory exercise in Python, after all). Which is what Hackerrank is about.

A sort of fair point. But then another one pointed out:

“I agree that this is more of a maths test than a coding test. However after having done a few of the challenges on here I’m starting to understand that maths is more an integral part of programming than I originally believed. Not all challenges we face are text based and some can only be solved with creative maths (even if an easy formula exists) to overcome memory and storage limitations etc. So I think we need to welcome maths challenges even if the resulting code is easy.”

Now, this brings me to the actual question. Is coding just all about… coding? I mean basically, they are mostly logic, right? Syntax, the structure of code, etc, all differ from language to language, but a logic of an algorithm is the main thing in my belief. In fact, there are cases where a mathematical solution to a problem is way more efficient than making the machine to use brute force to solve something. But as a programmer, do you prefer to use the power of computation of machine(which is sort of the main benefit humans have always looked into) or you prefer to make use of both- the human’s brain and the artificial one’s capability as well.

Conclusion

So do you believe Person 1 was indeed right or you are more on side of the second one? And here’s a solitary meme I found relevant to this post. Enjoy!

--

--

Amit Amola
Amit Amola

Written by Amit Amola

An iota among complex ones, a line among conics and small multi-cellular within this vast universe — just trying to grow!

No responses yet