Google Interview Question for Software Engineer / Developers


Country: United States




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

uint bit_count(uint value)
{
	printf("0x%08X has ", value, value);
    value = (value & 0x55555555) + ((value & 0xaaaaaaaa) >> 1);
    value = (value & 0x33333333) + ((value & 0xcccccccc) >> 2);
    value = (value & 0x0F0F0F0F) + ((value & 0xF0F0F0F0) >> 4);
    value = (value & 0x00FF00FF) + ((value & 0xFF00FF00) >> 8);
    value = (value & 0x0000FFFF) + ((value & 0xFFFF0000) >> 16);
    printf("%d bits set.\n", value);
    return value;
}

- Simon September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

Forgot to mention that this has the advantage of being O(log(n)) rather than O(n) in the number of bits used by value type.

- Simon September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain the logic behind this code ?

- Anonymous September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This would have many more upvotes if anyone except a couple people understood what's happening here.

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

Ok this is really an awesome trick !! :)

- Rishabh September 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can anybody please explain whats happening here

- ankit September 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We're taking all the odd bits and all the even bits, and then shifting the odd bits over so they're in the even positions. So if we had (for 8 bits, say), 11010011, we'd now have 10000010 (odd bits) >> 1 = 01000001, and 01010001 (even bits). Now we'll just add the two, but because of the way this has been structured, there's guaranteed space for the carries.

So we end up with 10 01 00 10. Each group of 2 digits here represents the count of 1s in the corresponding two digits. Now we add each group of 2 digits together
10 00 00 00 (odd groups of 2 - 1st, 3rd groups of 2) >> 2 = 00 10 00 00

00 10 00 00
00 01 00 10 (even groups of 2 - 0th, 2nd groups of 2)
------------

0011 0010
Each group of 4 has the count of 1s in that group of 4 bits.

A final step will similarly sum 0011 + 0010 = 0101. This is the final answer of 5 set bits.

I'm still impressed by how clever this trick is.

- eugene.yarovoi September 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lots of other bit related hacks:
http :// graphics.stanford.edu/~seander/bithacks.html

- pckben October 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's worth mentioning this is existing algorithm for Hamming weight problem.

- Iuri Covalisin November 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
12
of 12 vote

int count=0;
while(n>0){
//n-1 will set the right most set bit to zero. AND with the same number will sustain the remaining bits
n&=n-1;
count++;
}

the value of count gives the number of count bits.

- dreamCoder September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

And then I think the question wants you to cache that value to speed up the process in case the function is called with the same input again.

- eugene.yarovoi September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is it possible to cache all answers ??

- Anonymous September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Nicely done. But, do you need to & with (n-1). Can't you & with 1?

Something like this?

public class F {

  private static final int NUM = 85;

  public static void main(String[] args) {
		
    int num = NUM;
    int count = 0;
    int x = 0;
		
    while (num > 0) {
      count = count + (num & 1);
      num = num >> 1;
    }
		
  System.out.println("Count = " + count);
		
}
}

- Prash October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Prash: the original solution has complexity O(number of set bits); your solution has complexity O(number of set bits + number of not-set bits)

- eugene.yarovoi October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

class Test{
	public static void main(String args[]){
		System.out.println("Enter number");
		Scanner scanner=new Scanner(System.in);
		int number=scanner.nextInt();
		int count=0;
		while(number>0){
			if(number%2!=0)
				count++;
			number/=2;
		}
		System.out.println("number of set bits are:"+count);

	}

The complexity would be O(logn).

- Anonymous September 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you explain why shifting the number right is logn complexity? worst case you'll shift all bits, which is O(n)

- Iuri Covalisin November 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

They probably meant it's log(number), which is true. It's logarithmic in the value of the number, which is the same as saying it's linear in the number of digits of the number.

- eugene.yarovoi November 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Read programming pearls...

- Anonymous September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the key here is to improve time second time via "unlimited memory". Means interviewer tried to get better complexity second time. It's possible to achieve O(1) complexity second time if we use enourmous ammount of memory, by allocating an array with type max value size, for int that would be 0xffffffff, which is ~16GB at 32 bit machine which is impossible to address. But we have unlimited memory, so here's the code. The way to make it real is use bitset for cache, as we need only 5 bits to store bits count for 32 bit type.

size_t cache[0xffffffff] = {0};

size_t countBits(unsigned int i) {
    if(i == 0) return 0;

    // cache lookup in O(1)
    size_t count = 0;
    if(cache[i] != 0)
        return cache[i];

    // calculate
    while(i > 0) {
        i &= i - 1;
        count++;
    }
    // and cache the result
    cache[i] = count;
    return count;
}

- Iuri Covalisin November 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Can you explain in little more detail?

- Andy2000 September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Suppose, the number n is 7 (in binary 0111) and n-1 will be 6 (0110)
n&=n-1 in the first iteration will be 6 (count incremented to 1)
in the second iteration it will be (0100) count incremented to 2 and in the third iteration n will be 0000 and count incremented to 3 and the loop exits as n will go less than 0 after this. Now, the count has the number of bits set.

- dreamCoder September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Good code. For other folks. Here is the binary representation of few numbers:

binary: 0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = 10 1011 = 11

- Andy2000 September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

the given code is correct.
Do you mind if I ask in what round of Google interview is this question asked?

- Anonymous September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

the given code is correct.
Do you mind if I ask in what round of Google interview is this question asked?

- Anonymous September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

the given code is correct. ..
Do you mind if I ask in what round of Google interview is this question asked?

- Anonymous September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

These are the questions from Intern interview that my friend had

- Andy2000 September 04, 2012 | 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