Amazon Interview Question for Software Engineer / Developers






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

int Ceiling2(int x){

while(x & (x-1)){
// Removing the extra set bit.
   x &= (x-1);
}
return x<<1;

}

- Ankush Bindlish January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This looks correct.

- Prince January 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect.

- Ajit January 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try your function with 4, 8, 16, ...
you will get 8, 16, 32, ... which is wrong.
it should return 4, 8, 16, ...

- Anonymous January 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int Ceiling2(int x){

if (x && !(x & x-1))
 return x; //power of two
else{
 y=x;
 while(y & (y-1)){
  // Removing the extra set bit.
  y &= (y-1);
  }
 return y<<1;
}

}

- Anonymous February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gr8!

- luvtheedragon February 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous You are wrong.
if 4,8,16...are given as input then output should be 8,16,32...
The question asks for "the next higher power of 2".
@Ankush Bindlish Gr8 soln

- coderBon August 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent solution

- bhargav October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void main()
{
   int n,num=1;
   cout<<"Enter number";
   cin>>n;
   while(n)
   {
     n>>=1;
     num<<=1;
   }
   cout<<num;
}

- coderBon August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Round up log_2(k)

- Silence December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find the MSB position , then set MSB+1 position to 1 and all other positions to 0

- Anonymous December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int roundOff(const int n) {
if (n == 0 ) {
return 1;
}
int k = n;
int i = 1;
while(true) {
k = k >> 1;
i = i << 1;
if (k == 0) {
return i;
}
}
return 0;
}

- Anonymous December 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int roundOff(int number){
        int maxBit = 0;
        int mask = 1;

        while(number != 0){
                number >>= 1;
                maxBit++;
        }

        mask <<= maxBit;
        return mask;
}      

int main(){
        printf("%d\n",roundOff(1));
        printf("%d\n",roundOff(2));
        printf("%d\n",roundOff(3));
}

- snowbeard January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try
printf("%d\n",roundOff(0));

output is 1.

- kulang January 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And your point is?

- snowbeard January 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int roundpow(int num)
{
if( num <=0 )
return -1;
if( num & num-1 )
return num;

while( num & num+1 )
{
num++;
}
return num+1;
}

- AAD January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n ^ n + 1
i.e.
add xor of the number to itself, then add 1

- Anonymous January 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

(n OR n+1)+1

- Prabhu January 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check the case when n=24 ,lets assume 8 bit numbers ,24=00011000 24 OR 25 would be 00011001 adding 1 to it gives 00011011.While the answer got to be 32 ie 00100000

- Ran January 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

exactly, ran is correct. n by the way, is it a must that v need to use bit wise operators only?

- Anonymous January 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(x%2!=0)
int q=x/2+1;
else q=x/2;
return (pow(2,q));

- Anonymous January 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Ankush Bindlish,

Try your function with 4, you will get 8 which is wrong. it should return 4 only.

Below function will work good:

unsigned int
CeilingToPowerOfTwo (unsigned int n) {
unsigned int mask = 1;
while (mask < n) {
mask <<= 1;
}
return mask;
}

- Vivek January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not sure it is optimized or not ..
1. Given a number, find its binary equivalent.
2. Count the length of the binary number.
3. Result = pow(2, length)

Eg, 5 = 101 ; length = 3
Result = pow(2,3) = 8

- chandra January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The function takes n as the input and rounds it to the next power of 2.
int pow(int n){
int i=1;
while(i<=n)
i<<1;

return i;
}

- Ran January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return Math.pow(2,Math.ceil(log x)+1);

- Rocky January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

log x is of base 10

- Anonymous January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

log x is of base 2

- Anonymous January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ofcourse, log x is of base 2

- Anonymous January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this wont work for 1,2,4,8,16,....2^n

- Anonymous January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ran is correct except that while loop should be less than [not less than or equal to]
int pow(int n){
int i=1;
while(i<n) // Change here instead of i<=n which makes it invalid for n = 2^k
i<<=1;

return i;
}

- Rocky January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup, that is the most elegant solution.

- sgt January 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And a small left shift typo here: i<<1

- XeqtR January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main() {
int i=0;
int nearestpowoftwo = 1;
while(givenNumber > nearestpowoftwo) {
i++;
nearestpowoftwo = powoftwo(i);
}
givenNumber = nearestpowoftwo;
printf("%d ", givenNumber);
return 1;
}

int powoftwo(int index) {
int ans=1;
while(index) {
ans = ans * 2;
index --;
}
return ans;
}

- Anonymous January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be one of teh solutions:

if ((x&(x-1)) == 0)
   return x;
while(x&(j))
   j = j+1;
return j;

- Vivek January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here j=x before while loop

- Vivek January 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here j=x before while loop

- Vivek January 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about

(n | ~n) + 1

?

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

Func(N)
{
int i = 1;
while(N>>2 - 1)
{
i++;
N = N >> 2;
}
return 2<<i;
}

- Anonymous February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find the most significant bit k (wiki Most_significant_bit)

return 2^(k+1)

I think if you input 4, you should receive 8.

int nearestPowerOfTwo(int number){
int position = 0;
while(((number >>= 1) | 0) != 0){
position += 1;
}
return (int) Math.pow(2, position + 1);
}

- Anonymous February 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n|~n +1) wont work..since all the 32 bits be set when u do n|~n

- Rocky February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n|~n +1) wont work..since all the 32 bits will be set when u do n|~n

- Rocky February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

whats the complexity of the solution

- Anonymous February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(n!=0){
n = n>>1;
counter++;
}
n = 1;
while(counter!=0){
n = n<<1;
counter--;
}

- Saurabh Shah February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(n&(n-1))n++;

- Saurabh Shah February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class next {


public static void main(String[] a)
{
boolean h = true;
int c =0;
int f = 201;
while(h ==true)
{
c++;
if((Math.pow(2, c))>=f)
{
System.out.print((int)Math.pow(2, c));
h = false;
}
}

}
}

- Anonymous May 15, 2010 | 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