In my recent interview, the interviewer asked me to
write a Java program to find the least number whose square is of form 1_2_3_4_5_6_7_8_9_0. Where "_" could be any 1 digit number.
And I’d stuck in that.
Can anybody help me with the logic to be implemented?
Well, the minimum number of that format is
1020304050607080900which has square root1010101010.10.... And the maximum number of that format is1929394959697989990which has square root approx.1389026623.11.Start at the lower bound, and iterate through to the upper bound. You use regex or even rudimentary string character matching, just check that the first char is 1, the 3rd char is 2, etc.
Also, I think a
longwould be sufficient for this.EDIT:
I just ran this on my machine, it took around 2 minutes. I suck at regex so I did it primitive style.
The result was
1929374254627488900(this is the squared number). Therefore, the root number is1389019170. Also note this is the only number I found matching the pattern, not just the minimum.