Bloomberg LP Interview Question for Software Engineer / Developers






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

Count = 0;
temp = num;
while(temp != 0)
{
if((temp & 1) == 1)
{
count++;
temp = temp>>1;
}
}

- Nithesh January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

temp = temp>>1; should be out side of the if statement.

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

divided by 2? (n%2 == 1)? Count++ : Count

- Xiao January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Xiao, not entirely correct, you are giving a solution to the number of 1's in the binary representation. but most likely the interviewer would have asked the number of bits high, when a byte is stored in memory where the number is stored in 2s complement. so only shifting would give the correct answer, most likely the left shift, as right shift would have problems with negative numbers.

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

Can you give an example so as to why divide by 2 will not work ?

- abhimanipal February 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nithesh is correct. But right shift always has problem with signed numbers. If it is an negative number, then right shift gets 1's coming from left rather than 0's. Thus, it would be better to use left shift of 1 as the bit mask instead.

- Kyra January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good point

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

@Kyra: Good point about the number being negative. So, one can always typecast the received number to an unsigned type before applying Nithesh's algorithm. That would be way easier than doing a left shift.

One other way would be to have look-up. Since 1 byte corresponds to 255 numbers, having a 255 entry table and then doing a table-lookup would be very fast.

- Bandicoot March 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main()
{
int num = 13;
int count = 0;
while(num!=0)
{
if((num & 256) == 256)
{
count++;
num = num << 1;
}
else
num = num << 1;
}
cout << count;
}

nitish's answer is missing the else. but i am still not entirely sure whether this answer is correct or not :|

- nebu January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess above should work.. btw, the if loop should be
if((num & 1) == 1) { count++; ..... }
num&256 would give the same number and num&1 would give 1 if last bit is 1 else 0.

- Rocky January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For an unsigned integer n:

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

- Anonymous January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int n=0x1;
int count=0;

for(i=0;i<31;++i)
{
count=count+ x&n;
x=x>>1;
}
printf("%d",count);

- bhajatin January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bitCounter(int number){
int counter = 0;
int n = sizeof(int) * 8;
int mask = 1 << (n - 1);
for(int i = 1; i <= n; ++i)
{
if((mask & number) != 0)
++counter;
number <<= 1;
}

return counter;
}

works for signed number

- Anonymous February 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A lookup table for the 255 bit patterns in a byte provides constant time performance.

- Wandering programmer March 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int number; //the number we'r considering.
int counter=0;
for(int c=number;c;counter++)
c&=c-1;
u'll get it..

- Yimin September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int byte;
int cnt = 0;
for(int i=0;i<8;i++)
if(byte & (int)pow(2,i))
cnt++;
cout<<cnt;

- Anonymous December 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n = (n & 0x55555555) + ((n & 0xaaaaaaaa) >> 1); //Only this line if 2 bits can represent the integer
n = (n & 0x33333333) + ((n & 0xcccccccc) >> 2); //This and all above if 4 (or less) bits can represent the integer
n = (n & 0x0f0f0f0f) + ((n & 0xf0f0f0f0) >> 4); //This and all above if 8 (or less) bits can represent the integer
n = (n & 0x00ff00ff) + ((n & 0xff00ff00) >> 8); //This and all above if 16 (or less) bits can represent the integer
n = (n & 0x0000ffff) + ((n & 0xffff0000) >> 16); //This and all above if 32 (or less) bits can represent the integer

So all above four lines can be used for any 32 bit integer.

- Anurag Singh January 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the best implementations of the above algorithm is that if we do a & operator with the digit 1 and then if the result comes to be 1 which means that the variable temp contains a bit which is 1 and then, we can increment the counter variable and then we can shift the number to right side and then again check till the number is not equal to zero.

Implementation:

#include<bits/stdc++.h>
using namespace std;
int findbits(int num){
  int count = 0; 
  int temp = num; 
  while(temp != 0){ 
    if((temp & 1) == 1){ 
        count++; 
        temp = temp>>1; 
    } 
  }
  return count;
}
int main(){
    cout<<findbits(3)<<endl;

}

- swapnilkant11 June 08, 2019 | 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