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;
}

- sulzandrei December 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- zr.roman December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- poojaarora014 December 23, 2015 | Flag
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;
	}
}

- madi.sagimbekov December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- cr.rusucosmin December 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- zr.roman December 24, 2015 | Flag
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;
        }
    }

- abhay0609 December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"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.

- zr.roman December 22, 2015 | Flag
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;
}
}
\\\

- sulzandrei December 23, 2015 | Flag Reply
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)

- SS December 24, 2015 | Flag Reply
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)

- SS December 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int power(int base, int ext) {
	
	int result = 1;
	for(int head = 1, tail = ext;head <= tail; head++, tail--){
		if(tail == head) {
			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);
}

- davie.lin76 December 24, 2015 | Flag Reply
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).

- erikseulean December 27, 2015 | Flag Reply
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;
    }

- baoxin.zhou January 04, 2016 | Flag Reply
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;
		System.out.println(" answer = "+ans);
	}
    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);
	}
}

- yash mehta January 06, 2016 | Flag Reply
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;

}

- mka January 30, 2016 | Flag Reply
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
int answer = 1;

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

answer = answer * answer * (exp % 2 == 0 ? 1 : num);

return answer;
}

- Charlie Tran February 04, 2016 | Flag Reply
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);
        }

- Charlie Tran February 04, 2016 | Flag Reply
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);

}

- Charlie Tran February 04, 2016 | Flag Reply
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);

- Charlie Tran February 04, 2016 | Flag Reply
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;
}

- engineous.sumit December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that would overflow for power more than 32

- Darkhan.Imangaliev December 23, 2015 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More