The interview was of duration 1 hour.
The interviewer seemed to be of young age and kept on calling me bro bro :P
However, I found it a bit unprofessional. Also everytime I wanted to explain my approach, He wanted me to run the code on the extreme test case(here it was 10^18)
Problem: Find the floor of the square root of a given number n . n belongs to (1, 10^18)
I came up with an optimal approach within 10-15 mins but later when I tried to test 10^18, I was unable to at last I had to use BigInteger.
I had a hard time using BigInteger for the first time. He let me google its operations, still it was hard.
I was able to solve it in the given time however, I felt he was not satisfied.
// n -> (1, 10^18)
private static BigInteger squareRoot(BigInteger n) {
BigInteger mid = BigInteger.ONE, start = BigInteger.ONE, end = n;
while (start.subtract(end).compareTo(BigInteger.ZERO) < 0) {
mid = start.add((end.subtract(start)).divide(BigInteger.valueOf(2)));
if (n.divide(mid).compareTo(mid) < 0)
end = mid.subtract(BigInteger.valueOf(1));
else {
if (n.divide(mid.add(BigInteger.valueOf(1))).compareTo(mid.add(BigInteger.valueOf(1))) < 0)
return mid;
start = mid.add(BigInteger.valueOf(1));
}
}
return mid;
}
public static void main(String[] args) {
System.out.println(squareRoot(new BigInteger("393")));
}Hope my experience helps!