Google Interview Question for Software Engineer / Developers


Country: United States




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

count = 0;
while(n = n&(n-1))
count++;

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

How about when n = 4. count will be zero in that case.

- Victor January 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its like this
count = 0;
while (n)
{
count++;
n = n & (n-1);
}

- fred January 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

int main ()
{

int n ;
int count = 0;

printf("Enter the number: " );
scanf ("%d", &n);

while(n) {
if(n&1 ) {
count ++;
}
n = n>>1;
}

printf("The number of 1's in the number are : %d\n", count);

return 0;
}

- annu December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can do this using bit manipulations:

smth like:
x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); // # of 1's in every 2 bits
x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); // # of 1's in every 4 bits
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4); // # of 1's in every 8 bits
and so on..

some optimizations also possible on the last steps

- Anonymous December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There's a 2nd part to the question as well !
Testing the function:
1. Int Range Test (feeding numbers greater than the 32 bit limit of INT)
2. Negative numbers test

Other suggestions ?

- code_monkey December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

> feeding numbers greater than the 32 bit limit of INT

haha.. and how you'd feed a number that is greater than MAX_INT into 32-bit int ? ))

- Anonymous December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using bit shifting:

def countbits(n):
    count = 0
    while(n):
        count += 1 & n
        n >> 1
    return count

Or using properties of binary:

def countbits(n):
    count = 0
    while(n):
        n = n & (n - 1)
    return count

Or you could do something recursive:

def countbits(n):
    if n == 0:
        return n
    return (1 & n) + countbits(n >> 1)

Or even (though not as pretty):

def countbits(n):
    if n != 0:
        n = (1 & n) + countbits(n >> 1)
    return n

- peterejhamilton January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Contract: Number -> Number
* Purpose: It will return no. of one's in the binary representation of the
* given no.
* Example: See Tests
* Strategy:Generative recursion */
public static int NumberOfOnes(int iVal){
//int i =iVal;
int ans =0;
if(1==(iVal % 2))
ans++;
while(!(0==iVal || 1==iVal)){
iVal=iVal/2;
if(1==(iVal % 2))
ans++;
}
return ans;
}

- Anonymous December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do i have to login to comment

- answer December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Populate an array of size 128 mapping 8 bit number -> # of 1's in it
2) Bit mask off the 4 sections of 8 bits in the 32 bit integer
3) Add up their respective entries in the array

- Anonymous December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is how we can test it.

2^n-1 should have n ones. Ex: 2^2-1 = 3 should have 2 ones
similarly 2^3-1 =7 should have 3 ones...

- shilpa January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

“How will you test this function” is the key point of the question.
So before writing code, we need to consider the possible exceptions. Obviously, here the integer could be positive, negative or 0. So the code should deal with all those conditions. The way of right shifting repeatedly is not available for negative integers because they are represented by complement + 1. So we can use left shifting. The code is shown as follows
public static int count(int n) {
int count = 0;
int flag = 1;
while (flag != 0) {
if ((n&flag) != 0)
count++;
flag = flag<<1;
}
return count;
}

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

“How will you test this function” is the key point of the question.
So before writing code, we need to consider the possible exceptions. Obviously, here the integer could be positive, negative or 0. So the code should deal with all those conditions. The way of right shifting repeatedly is not available for negative integers because they are represented by complement + 1. So we can use left shifting. The code is shown as follows
public static int count(int n) {
int count = 0;
int flag = 1;
while (flag != 0) {
if ((n&flag) != 0)
count++;
flag = flag<<1;
}
return count;
}

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

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int count=0,num;
	printf("Enter the Number:\n");
	scanf("%d",&num);
	while(num)
	{
		if(num & 1)
			count++;
		num = num >> 1;
		
	}
	printf("Count value of 1's in that number is %d \n",count);
	return 0;

}

- sakthivel Sethu,Software Engineer ,Bangalore October 16, 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