Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer Intern - Arabic Language - Zurich at Google:
Given an integer, write a c++ code that would count the number of bit positions that are set (1's)? What is the time complexity of your algorithm? Assume you were asked to improve it such that you would get the results instantaneously. What would you do?
See more for this Google Software Engineer Intern - Arabic Language - Zurich Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (1)
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 1 people found this helpful
by Jib:
int hamming(int myNumber)
{
int counter = 0;
int newValue = myNumber;
while (newValue != 0)
{
newValue = (newValue >> 1);
if (newValue & 0x01 == 0x01)
{
counter++;
}
}
return counter;
}
Assuming that we have enough memory, the best simpliest way to improve it is to do something like that:
static unsigned char wordbits[65536] = { bitcounts of ints between 0 and 65535 };
int hamming(int myNumber)
{
return( wordbits[myNumber&0xFFFF] + wordbits[myNumber>>16] );
}