Microsoft Interview Question for Software Engineer / Developers


Country: -




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

Get the 2's complement of the number by doing ~n +1
Then count the number of 1s using x&(x-1)

- adroitninja October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

No. of 1s in N's 2-complement = (32 or 64)- (No. of 1s in N)-(Index of LSB 1)+1

- Anonymous October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this should work... it is obvious that the solution for -ve no's is hardware dependent i.e. for 32 bit systems it is different then for same -ve no. on 64 bit system...


#include<stdio.h>

int main()
{
int a,b,in_a,in_b;

printf("\n Enter a : ");
scanf("%d",&a);

printf("\n Enter b : ");
scanf("%d",&b);

in_a=find(a);
in_b=find(b);

printf("\n No. of 1's in 2's compliment of a : %d",in_a);
printf("\n No. of 1's in 2's compliment of b : %d\n\n",in_b);

return 0;
}

int find(int a)
{
int ret=0;

if(a>0)
{
while(a)
{
if(a&1)
ret++;
a=a>>1;
}
}
else if(a==0)
ret=0;
else
{
a=~a;
ret=32; //here ret is maximum bits in the representation of number and it varies from machine to machine
while(a>0)
{
if(a&1)
ret--;
a=a>>1;
}
}

return ret;
}

sample input :
a=2
b=-7

output :
no.of 1's in 2's compliment of a : 1
no. of 1's in 2's compliment of b : 30

- kim October 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lets consider 32 bit machine.
and the question is to count 1's in all the number from a to b. a and b can be upto 2^31-1

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

This comment has been deleted.

- Administrator October 14, 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