## Microsoft Interview Question for Developer Program Engineers

• 0

Country: India

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

without parallel processing naive method is :-
simply % 2 to increase the count of 1 and then divide by 2(basically checking the last bit of the no till Most significant bit comes to last bit)

``````while(no!=0)
{
if(no%2 == 1)count++;
no = no/2;
}``````

With parallel processing ideal logic must be divide total no bits that your no can have into different set and parallel process each one of them

for eg if you have a 64 bit no divide 64 bit into 8 set of 8 bits

no1 = no&(2^8-1 <<0)
no2 = no&((2^8-1) << 8)
no3 = no&((2^801) << 16).
.
.
so on till no8

now for each no calculate separately in each processor the no of 1's and then add them.
..

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

one correctio:- n(typ)
no3 must be :- no&((2^8 -1)<<16)

generic
no x = no&((2^8 -1)<<8*(x-1))

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

given that number is loo large like "234592379873427345723452943852983745982345634" in the form of string or something else.
so how can you do no%2, as this not int / long...
you need to extract first 8 characters and find the #of 1's in it and then next 8 characters and so on...

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

count(int num)
{ int counter=0;

while(num>0)
{
num=num & num-1;
counter++;
}

}

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

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

#include<iostream>

using namespace std;

int main()
{
int n,count=0;
cin>>n;
while(n!=0)
{
count++;
n=n&(n-1);
// cout<<n<<endl;
}

cout<<count;
return(0);
}

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

what is complexity of this code.

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

Number of one' s present in 'n'

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

then how to make parallel call?

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

count the length create two or more start points like start comparing from 0th index to forwrd and nth index backward stop whre indexes r equal..(this way many start points can be created and can be executed simultaneously in threads)
run using thread to speed up

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.

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