Ebay Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Correct! I would use binary search. Here it goes:
public bool IsPerfectSquare(int n)
{
if (n == 1) return true;
if (n < 4) return false;
int min = 2;
int max = n / 2;
do
{
int div = (max + min) / 2;
int res = n / div;
if (res * res == n) return true;
if (res < div)
{
min = res;
max = div;
}
else
{
min = div;
max = res;
}
} while (max > (min + 1));
return false;
}
Just keep adding odd numbers.
0+1=1
1+3=4 etc etc
-------------------
int count = 0;
int oddInteger = 1; //So, odd integer will be 1, then 3, then 5, then 7, etc etc
while(count <= N){
if(count == N){
return true;
}
count = count + oddInteger;
oddInteger = oddInteger + 2;
}
Runs in O(Sqrt(n)) time I believe.
This is an intelligent solution, but still executes N/2 additions. Then, it runs in O(N). I would do a binary search (I posted the code on another answer).
bool isPerfectSquare(int N)
{
for(int i=0; i*i <= N; i++)
if(i*i == N)
return true;
return false;
}
This works, but I think they wouldn't accept a solution that runs in O(n). For N==2^31-1, this would take too long.
binary search of (0, x / 2)
bool is_sqrt_helper(int x, int from, int to)
{
if (to < from) return false;
int mid = (from + to) / 2;
int r = mid * mid;
if (from == to)
return r == x;
if (r > x)
{
return is_sqrt_helper(x, from, mid - 1);
}
else if (r < x)
{
return is_sqrt_helper(x, mid + 1, to);
}
else
{
return true;
}
}
bool is_sqrt(int x)
{
if (x == 0 || x == 1) return true;
return is_sqrt_helper(x, 0, x / 2);
}
{ float x = n;
float y = 1;
float e = 0.000001; /* e decides the accuracy level*/
while(x - y > e)
{
x = (x + y)/2;
y = n/x;
}
return x; }
it is Babylonian method for fiinding square root
Forgot to mention the constraint. solution should work in O(N) time.
This junk site not allowing to edit existing questions. Sorry for the late clarifications.
/*
This will run in logarithmic time.
*/
public boolean isPerfectSquare(Integer input) {
// note - this method ignores the possibility of integer overflwo.
boolean flag = false;
int value = 1;
// the number we multiply the upperlimit with as we search for the
// limits that bound the input from above and from below.
int factor = 2;
// the limits that bound the input from above and below
int lowerLimit = 0;
int upperLimit = 1;
// determine the limits within which binary search is to be done.
while (input > upperLimit * upperLimit) {
lowerLimit = upperLimit;
upperLimit = upperLimit * factor;
}
// the do th binary search
if (!flag) {
while (lowerLimit < upperLimit) {
value = (lowerLimit + upperLimit) / 2;
int valueSquare = value * value;
if (valueSquare > input) {
upperLimit = value-1;
}
else if (valueSquare < input) {
lowerLimit = value+1;
}
else {
flag = true;
break;
}
}
}
return (flag);
}
The below code runs in O(logN) time.
bool isSqrRoot(int n)
{
for(int i=1;i<n/2;i++)
{
if((i*i)==n)
return true;
}
return false;
}
public class PerfectSquare {
public static void main(String[] args) {
int n= 144;
PerfectSquare ins = new PerfectSquare();
System.out.println("is "+n+" is perfect square : "+ins.isPerfectSquare(n));
}
public boolean isPerfectSquare(int n){
for(int i=0;i*i <=n;i++){
if(i*i == n){
return true;
}
}
return false;
}
}
static void Main(string[] args)
{
Console.WriteLine("Result = " + calcSQRT(2)); //Test Call;
Console.WriteLine("End");
}
static Double calcSQRT(int square, int overAllPrecision = 32)
{
int[] pairArray = getPairsArray(square);
String resultBuffer = "";
int currentIndex = 0;
BigInteger A = new BigInteger(pairArray[pairArray.Length - currentIndex - 1]);//BigInteger is from the System.Numerics
BigInteger y = new BigInteger(0);
while (true)
{
int i = 0;
while ((A - (10 * y + 2 * (i + 1) - 1)) >= 0)
{
i++;
A = A - (10 * y + 2 * i - 1);
}
if (currentIndex == pairArray.Length)
resultBuffer = resultBuffer + ".";
resultBuffer = resultBuffer + i;
currentIndex++;
if (currentIndex > overAllPrecision) break;
if (A == 0)
if (currentIndex >= pairArray.Length)
break;
A = 100 * A;
if (currentIndex < pairArray.Length)
A = A + pairArray[pairArray.Length - currentIndex - 1];
if (currentIndex == 1)
y = 2 * i;
else
y = 10 * y + 2 * i;
}
return Double.Parse(resultBuffer);
}
private static int[] getPairsArray(int square)
{
int[] pair = new int[1];
int count = 0;
while(square > 0)
{
int digit = square % 10;
square = square / 10;
if(square > 0)
{
digit = (square % 10) * 10 + digit;
square = square / 10;
}
if (count > 0)
Array.Resize(ref pair, count + 1);
pair[count] = digit;
count++;
}
return pair;
}
static void Main(string[] args)
{
Console.WriteLine("Result = " + calcSQRT(2)); //Test Call;
Console.WriteLine("End");
}
static Double calcSQRT(int square, int overAllPrecision = 32)
{
int[] pairArray = getPairsArray(square);
String resultBuffer = "";
int currentIndex = 0;
BigInteger A = new BigInteger(pairArray[pairArray.Length - currentIndex - 1]);//BigInteger is from the System.Numerics
BigInteger y = new BigInteger(0);
while (true)
{
int i = 0;
while ((A - (10 * y + 2 * (i + 1) - 1)) >= 0)
{
i++;
A = A - (10 * y + 2 * i - 1);
}
if (currentIndex == pairArray.Length)
resultBuffer = resultBuffer + ".";
resultBuffer = resultBuffer + i;
currentIndex++;
if (currentIndex > overAllPrecision) break;
if (A == 0)
if (currentIndex >= pairArray.Length)
break;
A = 100 * A;
if (currentIndex < pairArray.Length)
A = A + pairArray[pairArray.Length - currentIndex - 1];
if (currentIndex == 1)
y = 2 * i;
else
y = 10 * y + 2 * i;
}
return Double.Parse(resultBuffer);
}
private static int[] getPairsArray(int square)
{
int[] pair = new int[1];
int count = 0;
while(square > 0)
{
int digit = square % 10;
square = square / 10;
if(square > 0)
{
digit = (square % 10) * 10 + digit;
square = square / 10;
}
if (count > 0)
Array.Resize(ref pair, count + 1);
pair[count] = digit;
count++;
}
return pair;
}
If you cannot use floating point approximations, do binary search.
- Anonymous April 05, 2014