Walmart Labs Interview Question
Software Engineer / DevelopersTeam: Services
Country: India
Interview Type: In-Person
This function takes care of a case where given number itself is perfect square. No need to add extra check for that.
To get the square root of a number if a float operation, it will be slower than other operation.What we need is an integer which is close to it's square root.We can use an algorithm like binary search to do this.
int get_closest_int_root(int n){
int low = 0 ,high = INT_MAX;
while(low < high){
int mid = ((low + high) >> 1);
if (mid * mid > n){
high = mid - 1;
}else if ( mid * mid < n){
low = mid + 1;
}else
return mid;
}
if (high*high > mid){
return (high*high - mid < mid - (high-1)*(high-1))?high:(high-1);
}else{
return ((high+1)*(high+1) - mid < mid - high*high)?(high+1):high;
}
Use divide and conquer to find the minimum square root.
public class ClosestPerfectSquare {
public int getClosestPerfectSquare(int x) {
int min = findMinSquareRoot(x, 1, x);
int a = min * min;
int b = (min + 1) * (min + 1);
if (x - a < b - x) {
return a;
} else if (x - a > b - x) {
return b;
} else {
return x;
}
}
private int findMinSquareRoot(int x, int left, int right) {
if (left > right) {
return -1;
}
if (left * left <= x && x <= (left + 1) * (left + 1)) {
return left;
}
int mid = left + (right - left) / 2;
int midSquare = mid * mid;
int midOneSquare = (mid + 1) * (mid + 1);
if (x > midOneSquare) {
return findMinSquareRoot(x, mid + 1, right);
} else if (x < midSquare) {
return findMinSquareRoot(x, left + 1, mid);
} else {
return mid;
}
}
public static void main(String... args) throws Exception {
for (int i = 1; i <= 100; i++) {
int result = new ClosestPerfectSquare().getClosestPerfectSquare(i);
System.out.println(String.format("%3d : %3d : %3d", i, result, (result - i)));
}
}
}
1. Take square root of the number.
2. if(decimal part of num >=.5)
3. take floor
4. else
5. take ceiling
6. square the number to get the desired result.
This is close, but doesn't work all the time. Take the number 10,100.49. The square root is 100.501~, so according to these steps we should round up to 101 and square it.
However 10,100.49 is actually closer to the square of 100 (10,000) then the square of 101 (10,201).
I am still trying to figure out the formula to mathematically get the correct number.
public class ClosestSquare{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the number here: ");
double x;
Scanner scanIn = new Scanner(System.in); //Wrap the System In with a SCANNER
x = Integer.valueOf(scanIn.nextLine());
scanIn.close();
if (Math.floor(Math.sqrt(x)) == Math.ceil(Math.sqrt(x))){
System.out.println("The given number x: " + x + " is a perfect square...");
}else{// The given number is NOT a perfect square so let's find the closest one
double xSmall = Math.floor(Math.sqrt(x))*Math.floor(Math.sqrt(x));
double xLarge = Math.ceil(Math.sqrt(x))*Math.ceil(Math.sqrt(x));
if (Math.abs((x-xSmall)) > Math.abs(x-xLarge)){
System.out.println(("The closest perfect square is: " + xLarge));
}else{
System.out.println(("The closest perfect square is: " + xSmall));
}
}
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,t=m=0;
clrscr();
printf("Enter the number");
scanf("%d",&n);
while(1)
{
i=n;
j=n;
i++;
j--;
t=sqrt(i);
m=sqrt(j);
if(i==t*t)
{
printf("Nearest Perfect Square is:%d",i);
break;
}
if(j==m*m)
{
printf("Nearest Perfect Square is:%d",j);
break;
}
}
getch();
}
Is there any problem with this approach
int i,j,n,avg1;
float avg2;
scanf("%d",&n);
i=n;
j=n;
while(1)
{
i++;
j--;
avg1=sqrt(i);
avg2=sqrt(i);
if(avg1==avg2)
{
printf("the near perfect square is %d ",i);
break;
}
avg1=sqrt(j);
avg2=sqrt(j);
if(avg1==avg2)
{
printf("the nearest perfect square is %d ",j);
break;
}
}
- Zonito August 26, 2012