Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 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 May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thumb up!

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

How is this correct? You are not checking if c is 1 or 0 before increment count.

- diva.raj91 October 14, 2014 | 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 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 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 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 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 .... 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 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 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 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 August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here size of int is 2 bytes.... so i started pos from 15( left to right )

#define isOne(num,pos)  (( num & ( 1 << pos ) ) ? TRUE : FALSE )

int flipCounter( int num1, int num2 ) {
   for( pos = 15 ; pos != -1 ; pos-- )
      if( isOne(num1,pos) != isOne(num2,pos) )   count++;
   return count;
}

- get2jawa July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi! , Everyone loves a persons writing quite definitely! promote many of us keep up a correspondence more info on your own document for Yahoo? I need a pro for this dwelling to unravel this dilemma. Could be that is definitely a person! Looking forward to search an individual. dgdbfbedakaddbba

- Smithe878 March 13, 2015 | 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