Google Interview Question for Software Engineer / Developers






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

to count the number of bits in O(1), you need preprocessing, i.e., build a lookup table for 0, 127. For example lookupt[0] = 0; lookupt[1] = 1; lookupt[2] = 1; lookupt[3] = 2; ...

Given a 32-bit number, you get the number of bits,
unsigned int MASK = 0xFF;
return (lookupt[num & MASK] + lookupt[num >> 8 & MASK] + lookupt[num >> 16 & MASK] + lookup[num >> 24 & MAKS];

- Anonymous November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is is still O(n). The number of terms in you're sums grows linearly with number of bits.

- langeolli January 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"improving" your algo to count the number of bits in O(1) could be a trick question. The most straightforward algorithm that simply goes through the integer bit by bit (shift left the mask, AND it with the number) is already O(1). The reason for this is that it's asymptotically bound by a constant function f(n) = sizeof(int)*8

- Alex November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* Returns the number of bits set in val.
* For a derivation of this algorithm, see
* "Algorithms and data structures with applications to
* graphics and geometry", by Jurg Nievergelt and Klaus Hinrichs,
* Prentice Hall, 1993.
*/
private static int bitCount(long val) {
val -= (val & 0xaaaaaaaaaaaaaaaaL) >>> 1;
val = (val & 0x3333333333333333L) + ((val >>> 2) & 0x3333333333333333L);
val = (val + (val >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
val += val >>> 8;
val += val >>> 16;
return ((int)(val) + (int)(val >>> 32)) & 0xff;
}

- anup January 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I know this solution. But strictly speaking, I don't think it is O(1) operation.
It is still proportional to the number of bits that are needed to represent this number.

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

It's O(1), there are no cycles or recursion. This function code runs exactly one time.

- runnig March 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

which is O(lg n)

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

just take log base 2 of the number floor it and add 1.

- Rohan October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Roshan, your soln fails for this example
8 dec = 0000100 binary = 1 bit
log2(8) = 3+1 = 4

- bitmapper December 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Roshan, your soln fails for this example
8 dec = 0000100 binary = 1 bit
log2(8) = 3+1 = 4

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

This is a very old Question check this link out the stanford site (google Bit Twiddling Hacks)

- Garnie December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(1)....go for Hashmap

- Anonymous March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(1) is a constant time. So a for loop bit by bit is also O(1). But the constant factor can be optimized by using other tricks. HashMap can have the best perf but lot of space requirements. refer to (http)gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/ for some fast pieces....

- anony April 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if((n & n-1) == 0){
      return (int)log2(n)+1;
}
return (int)log2(n)

- Reza January 07, 2013 | 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