Hi
this year i'm going to go on internship to Facebook. Has smb worked there? Any feedback?
Name:
Anonymous2012-03-18 18:10
That's cool, got any details about what you'll be doing yet?
Name:
Anonymous2012-03-18 18:45
>>1
Have you passed the technical interview yet? If not, you should be able to answer the following questions with no effort.
"Write a function sum2(n) that returns the sum of the of the binary digits in n. For example, sum2(7) should return 7."
If you, just most of /prog, requires more than 5 minutes to solve it, then I'd recommend that you take up something a tad bit easier. Like say, working a cash register at your local Target.
Name:
Anonymous2012-03-18 18:51
>>3
Correction. sum2(7) should return 3. This is because 7 in binary is 111. And the sum of 111 is 3.
Name:
Anonymous2012-03-18 18:52
>>4
looking at you and pointing at toilet and scrub brush
>>5
I can write a three line C/C++ function that would solve this problem.
Name:
Anonymous2012-03-18 19:05
>>6
To make life easier, just assume that it's a two complement machine and that the integer n is unsigned.
Name:
Anonymous2012-03-18 19:07
>>3 Write a function sum2(n) that returns the sum of the of the binary digits in n. For example, sum2(7) should return 7. int sum2(n) {
return n;
}
Name:
Anonymous2012-03-18 19:07
>>7
I forgot that you could probably google the answer. So I'm going to add the restriction that you can't have any kind of global and/or static variables in your solution.
unsigned int sum2(unsigned int n){
unsigned int c;
for (c = 0; n; c++) n &= n - 1;
return c;
}
Name:
Anonymous2012-03-18 19:35
| *
so 0 isn't considered a binary digit
/prog/ ftw
Name:
Anonymous2012-03-18 21:05
>>16 >>3,4 said ``sum of the digits'' and not ``number of digits''. Zeroes do not change the value of a sum. printf("%d %d %d %d\n", sum2(6), sum2(7), sum2(8), sum2(5000)); should print 2 3 1 5 as >>12,13,15 do. Maybe >>3,4 should have given an example that wasn't all ones.