Amazon Interview Question for Software Engineer / Developers






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

#include "ConditionalCompilationMacro.h"
#include <stdio.h>

static int count = 0;

unsigned int APowerN(unsigned int A, unsigned int N)
{
int tempA = 0;

if(N > 1)
{
tempA = A;
A = A*A;
A = APowerN(A, N/2);
//A *= APowerN(tempA, N%2);
if(1 == (N%2))
{
A *= tempA;
count++;
}
count++;
}
else
{
if(0 == N)
{
count++;
return 1;
}
else if(1 == N)
{
count++;
return A;
}
}

return A;
}
void main()
{
unsigned int a = 2;
unsigned int N = 0;
unsigned int result = 0;
while(1)
{
count = 0;
printf("\nEnterN:");
scanf("%u",&N);
result = APowerN(a,N);
printf("\ncount:%u\n",count);
printf("\nResult:%u\n",result);
printf("\n**************\n");
}

}

- Sundar July 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since the overhead of recursive method, I think it is not very that optimal, use iterative method will be better

- Anonymous July 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ sunder ya io think it is optimal :)
approx 0(lgn)

- geeks July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use memoization.

- superstart July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is near optimal. O(logN)

int power(int a,int n) //n>=0 calculate a^n, for example n=6
{
int count=0,rev_n=0,ret=1;
while(n>0){ //reverse n (n=6 rev_n=3 110->011)
rev_n=(rev_n<<1)+(n&1);
n=n>>1;
count++; //count store how many bits. here count=3
}
while(rev_n>0){ //scan rev_n bits from right
ret=ret*ret*(rev_n&1?a:1);
rev_n =rev_n>>1;
count--;
}
for(;count>0;count--) //rev_n might have fewer bits than n
ret=ret*ret; // 011 needs additional squared calculation
return ret;
}

- hyperyan July 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

paste again for white space

//O(logN)
int power(int a,int n) //n>=0 calculate a^n, for example n=6
{
  int count=0,rev_n=0,ret=1;
  while(n>0){ //reverse n (n=6 rev_n=3 110->011)
    rev_n=(rev_n<<1)+(n&1);
    n=n>>1;
    count++; //count store how many bits. here count=3
  }
  while(rev_n>0){ //scan rev_n bits from right
    ret=ret*ret*(rev_n&1?a:1);
    rev_n =rev_n>>1;
    count--; 
  }
  for(;count>0;count--) //rev_n might have fewer bits than n
    ret=ret*ret; // 011 needs additional squared calculation
  return ret;
}

- hyperyan July 22, 2011 | 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