Amazon Interview Question Software Engineer / Developers




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

zhaoyangster that looks correct.
I have one variation/minor improvement to suggest, use c=c&c-1 instead of c=c>>1 and you only loop once for each bit, instead of all the bit positions.
{{
int bit_swaps_required( int a, int b ) {
unsigned int count = 0;
for( int c = a ^ b; c != 0; c = c & c-1 ) {
++count;
}
return count;
}
}}

- Developer on May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thumb up!

- pansophism on May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

saw the solution before: Use XOR

int bit_swaps_required( int a, int b ) {
unsigned int count = 0;
for( int c = a ^ b; c != 0; c = c >> 1 ) {
count += c & 1;
}
return count;
}

- pansophism on May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your approach is right. XOR the two numbers. And now we need to find number of 1's in this number. There are various efficient ways to find the number of 1's in any number. Just google it.

- Anu on May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Developer your code is working perfectly.Can u explain the logic behind c=c&c-1?

- kumarasvn on May 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it takes out all the 1's from RHS

- siddharam.s.t on May 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int num_bits(int x,int y)
{
int a,b;
a=x^y;
b=count_ones(a);
return(b);
}
int count_ones(int x)
{
int count;
while((n&(n-1))!=0)
count++;
return(count);
}

- Anna for .... on May 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bitswap(int a,int b)
{
    int c = a^b;
    int count = 0;
    while(c!=0)
    {
       if(c&1 ==1)
       count++;
       
       c = c>>1;
    } 
    return count;
}

- nihaldps on February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int num_bits(int x, int y)
{
int count = 0;
while(x != 0 || y != 0)
{
if((x&1)!=(y&1))
{
count++;
}
x = x>>1;
y = y>>1;
}
return count;
}

- Meena on July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bit_flip(int x, int y)
{
int count=0;
z=x^y;
while(z)
{
z&=z-1;
count++;
}
return count;
}

- Meenu on July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int flipBits(int a, int b){
        int c = a ^ b;
        int count =0;
	while(x!=0){
		if((x&1)==1){
			count++;
		}
		x= (x>>>1);
	}
        return count;
    }

it goes like:
1) XOR 2 numbers
2) count the number of 1's

I didn't check it for signed integers. Please correct me if am wrong somewhere.

- Anonymous on August 22, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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