Amazon Interview Question for Software Engineer / Developers


Country: United States




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

int pow(int A,int n)
{
    if(n==0)
            return 1;
    if(n==1)
            return A;
    else if(n%2==0)
         return (pow(A,n/2)*pow(A,n/2));//use DP to reduce the repeated calls
    else
        return (pow(A,n/2)*pow(A,n/2)*A);
}

- Abhishek March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The complexity of this algorithm is actually O(n). The depth of the call tree is log n, but the total number of multiplication remains (n-1).

- Anonymous March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Generally right idea, but can optimize a bit more

long pow(int A,int n)
{
    if(n==0)
            return 1;
    if(n==1)
            return A;
    
    long result = pow(A,n/2);
    if(n%2==0)
         return result * result;
    else
        return result * result * A;
}

- Anonymous March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I don't know why we are even talking about O(logN), if both the numbers are 4 byte integers, logN cannot be greater than 31. Working on that, here is the O(1) version of the algorithm. pow(a,b) where b is the power a has to be raised to. If b is taken to be s1s2u1u2s3...sN, where s indicates a set bit and u indicates an unset bit, then pow(a,b) will be equal to a^(2^s1)*a^(2^s2)*a^(2^s3)....a^(2^sN) where s1, s2..sN are the positions of the set bits of b.
The corresponding code (in java) is:

public static String pow(int a, int b)
    {
        int lsb= 0x1;
        BigInteger result= new BigInteger("1");
        BigInteger exponent= new BigInteger(""+a);
        while(b>=lsb)
        {
            if((b&lsb)>0)
                result= result.multiply(exponent);
            lsb= lsb<<1;
            exponent= exponent.multiply(exponent);
        }
        return result.toString();
    }

- nj September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Exponention is the way to go.
complexity..O(log(n))

- Anonymous March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you need to implement big numbers

- Anonymous March 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know why we are even talking about O(logN), if both the numbers are 4 byte integers, logN cannot be greater than 31. Working on that, here is the O(1) version of the algorithm. pow(a,b) where b is the power a has to be raised to. If b is taken to be s1s2u1u2s3...sN, where s indicates a set bit and u indicates an unset bit, then pow(a,b) will be equal to a^(2^s1)*a^(2^s2)*a^(2^s3)....a^(2^sN) where s1, s2..sN are the positions of the set bits of b.

The corresponding code (in java) is:

public static String pow(int a, int b)
    {
        int lsb= 0x1;
        BigInteger result= new BigInteger("1");
        BigInteger exponent= new BigInteger(""+a);
        while(b>=lsb)
        {
            if((b&lsb)>0)
                result= result.multiply(exponent);
            lsb= lsb<<1;
            exponent= exponent.multiply(exponent);
        }
        return result.toString();
    }

- Anonymous September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Make the function tail recursive

function tailRecurse(A,n)
{
if(n==0)
return 1;
if(n==1)
return A
else 
return (A*tailRecurse(A,n-1))
}

- Vishi March 29, 2012 | Flag Reply


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