Ebay Interview Question for Analysts






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

int bitcount (unsigned int n) {
int count = 0 ;
while (n) {
count++ ;
n &= (n - 1) ;
}
return count ;
}

- Addy July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very very good idea. Thanks a lot.

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

can any one explain the logic which makes the algo true.

- websitedesigner06 October 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int no_of_ones(int n)
{
iny cnt=0;
while(n>0)
{
if(n&1)
cnt++;
n=n>>1;
}
return cnt;
}

- idea July 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It should be a crime to ask this question anymore in interviews.

- knap July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats 100% right!! It has already expired its credintial as an interview Qs.

- anonymous July 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If I remember it correctly, this question is in K&R (Kernighan & Ritchie)

- Jinxed February 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks anny for gave good solution

- nav July 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countOnes(int x) {
    int count = 0;
    do count += x & 1; while (x >>= 1);
    return count;
}

or:

int countOnes(int x) {
    return (x & 1)
            + ((x & 2) == 2)
            + ((x & 4) == 4)
            + ((x & 8) == 8)
            + ((x & 16) == 16)
            + ((x & 32) == 32)
            + ((x & 64) == 64)
            + ((x & 128) == 128)
            + ((x & 256) == 256)
            + ((x & 512) == 512)
            + ((x & 1024) == 1024)
            + ((x & 2048) == 2048)
            + ((x & 4096) == 4096)
            + ((x & 8192) == 8192)
            + ((x & 16384) == 16384)
            + ((x & 32768) == 32768)
            + ((x & 65536) == 65536)
            + ((x & 131072) == 131072)
            + ((x & 262144) == 262144)
            + ((x & 524288) == 524288)
            + ((x & 1048576) == 1048576)
            + ((x & 2097152) == 2097152)
            + ((x & 4194304) == 4194304)
            + ((x & 8388608) == 8388608)
            + ((x & 16777216) == 16777216)
            + ((x & 33554432) == 33554432)
            + ((x & 67108864) == 67108864)
            + ((x & 134217728) == 134217728)
            + ((x & 268435456) == 268435456)
            + ((x & 536870912) == 536870912)
            + ((x & 1073741824) == 1073741824)
            + ((x & -2147483648) == -2147483648);
}

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

The logic is to iterate from 1 to 31 and right shift 1 each time. Do an AND with the original number. If the result is non zero then the bit is 1, so increment the count.

int CountOnes (int n)
{
int i,count=0;
for(i=1;i<32;i++)
{ if(n&(1<<i)!=0) count++;
}

cout<< count;

}

- Rochak June 13, 2011 | 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