## Walmart Labs Interview Question for Java Developers

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

``````public static int powerRecursive(int num,int pow) {
if (pow < 1)
return 1;
else
return num * powerRecursive(num, pow-1) ;
}
public static int pow(int base , int exp)
{

if(exp <= 1) return base;

int total = 1;

for(int i = 0; i < exp; i++)
{
total = (base * total);
}

}

Comment hidden because of low score. Click to expand.
1
of 1 vote

what if the task assumes that x and y can be of float or double type?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static int powerRecursive(int num,int pow) {
if (pow < 1)
return 1;
else
return num * powerRecursive(num, pow-1) ;
}
public static int pow(int base , int exp)
{

if(exp <= 1) return base;

int total = 1;

for(int i = 0; i < exp; i++)
{
total = (base * total);
}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we should use Divide and Conquer for efficiency when we impletement recursive version.

``````int powerRecursive(int num,int pow)
{
if (pow == 0)
{
return 1;
}

if (pow == 1)
{
return num;
}

int half = powerRecursive(num, pow/2);
if (pow % 2 == 0)
{
return half * half;
}
else
{
return half * half * num;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

good

Comment hidden because of low score. Click to expand.
0
of 0 vote

good

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static double power(double base, double basePow)
{

if(basePow==0)
{
return 1;
}
else if(basePow==1)
{
return base;
}
else if(basePow>1)
{
return base*power(base,basePow-1);
}
else
{

return 1/power(base, -1 * basePow);
}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````def pow(x,y):
if(y==0):
return 1
if(y==1):
return x
if(y<0):
return 1/pow(x,abs(y))
if(y%2==0):
r = pow(x,y/2)
return r*r
else :
r = pow(x,(y-1)/2)
return r*r*x
print(pow(3,-3))``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.