## Amazon Interview Question for SDETs

Country: India
Interview Type: In-Person

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

c++

int pow(int a,int b)
{
int res = 1;
while(b)
{
if (b&1)
res *=a
b >>=1;
a *=a
}
return res;
}

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

"o(logN) time complexity" - could you specify what is N here.

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

Here N is the power e.g. m^6 so N=6

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

long pow(int a, int b) {
long x = pow(a, b/2);
if (b % 2 == 0) {
return x * x;
} else {
return x * x * b;
}
}

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

Sorry, but this i O(logN) memory, since you have the calls ont he function saved on the stack.

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

sorry, but this is stackoverflow due to infinite number of self calls.

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

Java

public static float power(float x, int n){

if(n == 0) return 1;
float y = power(x,n/2);

if(n%2 == 0) return y*y;
else {

if(n > 0) return x*y*y;
else return (y*y)/n;
}
}

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

"if(n>;0)" and "else return (y*y)/n" are not needed, you cannot handle negative values of power here.
To handle negative values of power one more function is needed, but it is trivial.

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

C++
///
int pow(int a, int b)
{
int res = 1;
while (b)
{
if (b&1)
res *= a;
a *= a;
}
}
\\\

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

# In python.

def power(a, b):

# For an invocation where b = 7, this will get called 3 times.
print "Inside the power function \n"

# Default, return 'a'
# This will be done when b == 0 or b == 1
# (Also minimizing the number of return statements in the function).
retVal = a

# When b is a multiple of 2
if (b%2) == 0:
ret = power(a*a, b/2)
elif b > 2:
# When b is not a multiple of 2.
retVal = a * power(a*a, (b-1)/2)

return retVal

if __name__ == "__main__":
val = power(2, 7)
print "Output = "+str(val)

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

# In python.

def power(a, b):

# For an invocation where b = 7, this will get called 3 times.
print "Inside the power function \n"

# Default, return 'a'
# This will be done when b == 0 or b == 1
# (Also minimizing the number of return statements in the function).
retVal = a

# When b is a multiple of 2
if (b%2) == 0:
ret = power(a*a, b/2)
elif b > 2:
# When b is not a multiple of 2.
retVal = a * power(a*a, (b-1)/2)

return retVal

if __name__ == "__main__":
val = power(2, 7)
print "Output = "+str(val)

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

public int power(int base, int ext) {

int result = 1;
result *= base;
} else {
result = base * base;
}
}

return result;
}

// Recursive, but O(logN) space
public int power(int base, int exp) {
if(exp == 0)
return 1;
if(exp == 1)
return base;

boolean isExpOdd = exp % 2 == 1;
exp = exp / 2;

return power(base, exp) * power(base, isExpOdd ? exp + 1 : exp);
}

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

I think you're missing the cases when you have 12343 and you have to append 21 or
123432 and have to append only 1.
My solution would be to find the middle and try to check left and right until there's a missmatch. If I find a missmatch, it means the middle is incorrect so I increment it with 1 till I find the correct middle (both to the left and right I have the same values) or I hit the end (when none of the values match).

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

Java

public static int powN(int a, int n){
int res = 1;
int tmp = a;
while(n>0) {
if((n&1)>0) {
res=res*tmp;
}
n>>=1;
tmp=tmp*tmp;
}
return res;
}

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

Solution in java if any faults please let me know..

import java.io.*;
import java.util.*;
class power
{
public static void main(String args[])throws Exception
{
int base,exp;
long ans;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of base : ");
base = sc.nextInt();
System.out.println("Enter value of exponent : ");
exp = sc.nextInt();
if(exp%2==0)
ans = pow(base,exp);
else
ans = pow(base,--exp)*base;
}
public static long pow(int base, int exp)
{
if(exp==0)
return 1;
if(exp==1)
return base;
else
return pow(base,exp/2)*pow(base,exp/2);
}
}

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

int myPower(int a,int b)
{

int result  = 1;
int i,j;
int iteration = a*a*a;
j=0;

printf("starting %d^%d\n",a,b);
for(i=0;i<b;i+=3,j++)
{
result *= iteration;
printf("loop %d\n",j);
}
if(i==b+1)
{
result *=a;
}
if(i==b+1)
{
result *=a*a;
}
return result;

}

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

I see a lot of bit shifting and recursive calls. The questions is pretty straight forward. They are asking for O(log n) performance, which means you code it in a way that should do half the work. Just loop half the size of the exponent and multiply the number through. Then take the result and multiply it by itself.

public static int GetPower(int num, int exp)
{
// Leave default as 1 when the exponent is 0

for (int i = exp % 2 == 0 ? exp / 2 : (exp - 1) / 2; i > 0; i--)
{
}

}

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

public static int GetPower(int num,  int exp)
{
return exp == 0 ? 1 : exp == 1 ? num : GetPower(num, exp / 2) * GetPower(num, exp / 2) * (exp % 2 == 0 ? 1 : num);
}

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

public static int GetPower(int num,  int exp){return exp == 0 ? 1 : exp == num:GetPower(num, exp / 2) * GetPower(num, exp / 2) * (exp % 2 == 0 ? 1 : num);

}

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

public static int GetPower(int num, int exp)

return exp == 0 ? 1 : exp == 1 ? num : GetPower(num, exp / 2) * GetPower(num, exp / 2) * (exp % 2 == 0 ? 1 : num);

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

Fastest and easiest way to solve this problem with a space complexity of O(1) is:

Bitwise shift operator

int mult_by_pow_2(int number, int power)
{
return number<<power;
}

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

that would overflow for power more than 32

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.