Facebook Interview Question for Software Engineer / Developers






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

only count the number of one.

public int countOnes(int value)
	{
		int counter = 0;
		while (value > 0)
		{
			value = value & (value - 1); //去掉最后一个1
			counter++;
		}
		return counter;
	}

- lshmouse April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a little problem
while (value > 0) ==> while (value != 0)

- lshmouse April 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thats not O(1)...look for stanford;s link of bit hacks

- amm April 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why not be keep a table of size 2^8.
Each entry in table will hold number of bits in the number with number as the index into the table entry.
Solution then will be like this -->
Each number is composed of consecutive bytes, say 4 bytes.
1) Get the size of memory location holding the number.
2) right shift 8 bits from the number and store them in a variable, say X
3) update the number with right shifted value or use another variable to store updated value, say Y
4) index into the table of size 2^ 8 using X.
5) Repeat step step 2 to 4 using Y.

Now the size of a number is constant (even if it is machine dependent). So run time of this algorithm will be O(1).

- Abhishek Soni April 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like your solution but it also not O(1); it's O(b/8). Where b is number of bits.

If you argue and say that the number of bits itself is a constant (16 or 32 or 64 or whatever) and so O(b/8) is also a constant then why this fancy algorithm? In this case every algorithm someone could come up with (even the most trivial ones) would be constant time algorithms. Counting the 1-bits, or counting the number of times "value & (value - 1) != 0" can happen are all O(b) algorithms and thus would count as constant time algorithms the very same way...

- Safi December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main(){

int x=65536;
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
x = (x & 0x0000FFFF) + ((x >>16) & 0x0000FFFF);
printf("%d",x);
}

this is O(1) I think.

Since we got enough space.

int a [32]={1,1,1.....32 1's};
no_bits=n&a[0]+n&a[1]+......n&a[31];
return no_bits;

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

Hi Anonymous, Can you Please Explain The Algorithm, Please Through SOme Light On What u trying to doing..in each step ..clearly i will be very thankfull to you..plz explain this..

will wait for your response

Thanks
Algoseekar

- Algoseekar April 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,Algoseeker
Your question is directed to wrong person.Why the company ask such a Question (in this case O(1) is the catch) in the first place.Are they checking our mugging/cramming abilities?

- Anonymous April 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Anonymous, I m sure my question is directed to correct person ,Can you Please Explain The Algorithm, Please Through SOme Light On What u trying to doing..in each step ..clearly i will be very thankfull to you..plz explain this..

will wait for your response

Thanks
Algoseekar

- Algoseekar May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

shitty explanation !

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

This is just one of the Stanford bithacks. But you can tell them that the compiler vendors know them to, so your simple for-loop counting bits will usually be converted into it or even an arch specific instruction.

- FBNerd February 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no limit on memory. So just use a array map for each possibility.

- Rayden April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct

- Anonymous April 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then it would be a 2^32 element map... but it's a really O(1)

- Mirikle June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C# (this is the simplest solution but not O(1)):

public static int CountOnes(int value)
{
int counter = 0;
byte b = 1;

while (value>0)
{
if ((value & b) == b)
counter++;
value = value >> b;
}
return counter;
}

- cguirguis April 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O (lg n) where is n = 32 for 32-bit integer. For loop, other are discussing is O(n). Hash table is surely is O(1), but it unfeasible for 4 billions values.

lg n solution for 64-bit number will take lg n = lg 64 = 6 arithmatics (does not justify 2^64 hash entries).

unsigned int count_ones(unsigned int a)
{
    a = (a & 0x55555555) + ((a >> 1) & 0x55555555));
    a = (a & 0x33333333) + ((a >> 2) & 0x33333333));
    a = (a & 0x0f0f0f0f) + ((a >> 4) & 0x0f0f0f0f);
    a = (a & 0x00ff00ff) + ((a >> 8) & 0x00ff00ff);
    a = (a & 0x0000ffff) + ((a >> 16) & 0x0000ffff);
}

- Anonymous November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is always constant time. Your number can have at most as many bits as the architecture allows. It is not dependent upon how big or small the number is.

- Anonymous April 16, 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