## Amazon Interview Question for Developer Program Engineers

• 1
of 1 vote

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

Adding 1 to all 1s will produce a perfect power of 2
=> n & (n+1) == 0

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

@Ashish, I think you are trying to say n & (n-1)
8 = 1000
7 = 0111
& = 0000
Means if a number's all bits are 1, then doing the AND operation with previous number (number - 1) will result in zero.

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

No... since all bits in n are 1, adding 1 to it makes the sum a power of two. So I am correct in my code.

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

@ashish correct

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

But, 0 & (0+1) = 0. How about (n!=0) && (n&(n+1)==0) ?

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

n & (n+1) == 0

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

Well, unsigned. Like 7 for instance is 111.

If a number j is n bits in it's binary form, then do this:
j & (first n bits of j+1) == 0.

If that's true, then the number was all ones.

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

step1: negate the number (~q say number is q)
step 2: find xor of the two number(Q xor ~Q)
step 3: IF step2 result == Q all the every digit in binary is 1 else false

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

This is the most elegant solution. So it will be:

``if ((Q^(~Q))==Q)``

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

it should be
if((Q^(~Q))==-1)
{
then its all one
}

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

You guys are making a mistake. In 2's complement system,

``Q ^ (~Q) = -2``

In case of 1's complement, the result is 0 ( or -0, whatever that means ). In case of signed bit as the msb, the result is (-2^(k - 1) + 1), where k is the size of the integer in bits.

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

if(~num == 0) {printf("the num contains all ones\n");}

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

this will not work for 0x7fffffff - ~n is -1 or 0x80000000

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

Cant we just do an XOR with all 1's ???

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

if ( n XOR ffff ffff <> ffff ffff )
then n does not have all 1
else
n has all 1s

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

if ((n ^ 0xFFFFFFFF) == 0) {
printf("n has all 1's");
}
else
{
printf("n has some 0's");
}

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

if ((n ^ 0xFFFFFFFF) == 0) {
printf("n has all 1's");
}
else
{
printf("n has some 0's");
}

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

public class Allones {

public static void main(String args[]){

int no = Integer.parseInt("10", 2);

System.out.println((no&(no+1))==0);

}

}

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

if( (n& ~0)== ~0)
printf("\nAll bits set to 1\n");
else
printf("\nNot set to 1 all bits\n");

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

Sorry it shud be
if((n&~0)==(unsigned)(~0))
printf("\nAll bits set to 1\n");
else
printf("\nNot all bits set to 1\n");

Forgot to Typecast with Unsigned ;-)

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

i asked why im here when reading all these answers. here actually is the valid numbers,
1, 11, 111, 1111, 11111, ..., 11111111

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

return ~x == 0;

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

smffap you are very correct. i like your answer too

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

i have 2 similar answers. the 1st one is

``````if((x^o) == 1){       //x is the supplied number
system.out.println("this number "+x+" has all 1's in its bin.representation");``````

}
the second one is:

``````if((x^1) == 0){       //x is the supplied number
system.out.println("this number "+x+" has all 1's in its bin.representation");``````

}

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

The above algorithms can be solved by following that on every right shift of the binary number it's & operation with 1 should not be 0 if 0 then return true else till the number is greater than 0 return true if not found
Implementation:

``````#include<bits/stdc++.h>
using namespace std;
int findsetbits(int n){
while(n > 0){
if(n&1 == 0)
return 0;
n = n>>1;
}
return 1;
}
int main(){
int t, n;
cin>>t;
while(t--){
cin>>n;
if(findsetbits(n) == 0)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;``````

}

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

if (signednum == -1)

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

I guess n^n == 0 will also work.

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

this will not work for n=0

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

@Anonymous1 dude,this will work for every possible number.
Before posting answers , or rather don't post answers,fool!!

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

hahaa...legendary answer...the guy maybe is looking to disprove the identity

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

n^n is always 0, just go back and do some reading.

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

n ^ n will not work. It will always give 0 as an answer for every value of n.

01010101 n
01010101 n
------------------
00000000 n^n

The truth table of Xor Gate is below

11 0
10 1
01 1
00 0

---------------------------
Please guys lets not argue or use foul language. We all are here to learn.

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.