Amazon Interview Question for Developer Program Engineers






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

- Ashish Kaila June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- new2011 June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Ashish Kaila June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ashish correct

- WgpShashank June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

n & (n+1) == 0

- pansophism June 14, 2011 | Flag Reply
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.

- Anonymous June 14, 2011 | Flag Reply
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

- DNS June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

- Anonymous June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- davidit July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Jug January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- krishna June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

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

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous June 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- rockersatish June 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- rockersatish June 19, 2011 | Flag
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);

}

}

- Anonymous June 18, 2011 | Flag Reply
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");

- Prince July 13, 2011 | Flag Reply
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 ;-)

- Prince July 13, 2011 | Flag Reply
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

- lol October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ~x == 0;

- smffap December 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

smffap you are very correct. i like your answer too

- CAFEBABE December 27, 2011 | Flag
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");

}

- CAFEBABE December 27, 2011 | Flag Reply
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;

}

- swapnilkant11 July 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if (signednum == -1)

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work for n=0

- Anonymous June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ShutUp June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Gaurav September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- asamra1986 October 29, 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