Why My Code Would Take 316 Years to Execute by@gauravshankar

Why My Code Would Take 316 Years to Execute

@gauravshankarGaurav Shankar

QA turned Back-end @Freshworks👨🏾‍💻. Loves everything about trains and planes.

I started programming about 5 years ago. Ironically, only in the last year of my computer science degree. I was also made aware of the likes of Hackerrank and Hackerearth at the same time. I remember naively brute-forcing every single problem that I would come across on these platforms and later wonder why it would give me a TLE Error.

I wouldn't actually think of solving the problem then, but just do as said in the question and follow the initial intuition. The concepts of time and space complexity were equally alien to me as my approach to solve these. I wouldn't bother to look up the internet either about what really goes on in the code but just solve it. How stubborn!

Coming from a background where I took the CS degree just for the sake of it, with no real knowledge, needless to say, it's obvious that I knew nothing about real software development and how companies run. I was injudicious enough to believe that I can brute force myself into the companies that would come for jobs just like this.

It continued for a while until something happened which changed me, once and for all.

While I continued with my foolish approach, I stumbled upon this problem and I was in for a ride. I read the problem description and thought how easily I can solve it, by, obviously brute forcing it.

And I did!

And below were the results.

And this was my submission.

public class Solution {
static long luckyNumbers(long a,long b) {
long count = 0;
for(long i=a;i<=b;i++){
long number = i;
long currentDigitValue = 0;
long sumOutputValue = 0;
long sumSqOutputval = 0;
while(number != 0) {
currentDigitValue = number % 10;
number = number / 10;
sumOutputValue = sumOutputValue + currentDigitValue;
sumSqOutputval = sumSqOutputval + ( currentDigitValue * currentDigitValue);
}
boolean s1 = isPrime(sumOutputValue);
boolean s2 = isPrime(sumSqOutputval);
if(s1 == true && s2 == true){
count++;
}
else{
continue;
}
}
return count;
}
static boolean isPrime(long a){
boolean res1 = true;
if(a == 1){
res1 = false;
return res1;
}
else if(a == 2){
res1 = true;
return res1;
}
else{

for(long z = 2;z<a;z++){
if(a%z == 0){
res1 = false;
break;
}
else{
res1 = true;
}
}
if(res1 == true){
return res1;
}
else{
return false;
}

}
}
}

As usual, I kept wondering why would this happen but this time, for once I decided to do something about it. So I did the obvious and sought help from the community. And they responded as well.

And then it hit me hard!

Wha-at!?

My code would take 316 years to execute. How’s that even possible? I was shocked and desperate to get this fixed. Someone in the thread also suggested, the problem would need a 3D-DP approach to be solved, a term I had never heard of.

This was it. This incident propelled my curiosity and changed things in a way I'd have never imagined.
As the saying goes. . .

All it takes, is a little push.

It's needless to say that things didn't change for me overnight but It took a great amount of hard work to lift myself from the dirt I was in. Most importantly, it needs you to be curious about things. The day you stop being curious about it, you stop learning.

It's been so long since then, and I've really come ahead in my career but thinking about the incident, I wonder where I would've been if this wouldn't have happened with me.

I'm sure all of you would've been there too. What are your stories?

P.S: Oh! Did I mention that I was able to solve the same problem a few years later with all the test cases. Did you solve it too? Share your solutions.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.