Google Interview Question for Software Engineer / Developers


Country: United Kingdom
Interview Type: Phone Interview




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

Left shifting integer 1 (n-1) bits then xor it with the target.

int FlipBit (int m, int n) {
return m ^ (1 << n - 1);
}

- Ke February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

m^ pow(2, n-1)

I can't agree it more. Perfect answer.

- Danny February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you should also check to make sure n > 0. Otherwise this doesn't hold.

- ccc February 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good answer. Here are versions to set, clear and flip n-th bit of m:

int set_nth_bit(const int m, const int n)
{
    return m | (1 << n);
}

int clear_nth_bit(const int m, const int n)
{
    return m & ~(1 << n);
}

int flip_nth_bit(const int m, const int n)
{
    return m ^ (1 << n);
}

- artakbegnazaryan March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is assuming it's a big endian machine.

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

if ((n>>(m-1)) % 2==0))
return (n or 2^(m-1))
else
return (n xor 2^(m-1))

- Sanjay Kumar February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int func1(int n, int m)
{
if ( (n>>(m-1)) % 2==0)
return (n | (1<<(m-1)));
else
return (n ^ (1<<(m-1)));
}

- Sanjay Kumar February 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry do you mind explaining your solution a bit. I dont really know how to use these shifting bits left right and stuff

- llmorex February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

shifting n right by m-1 bit will bring m-th bit at lease significant position.

If the resultant number is even then m-th bit is 0, OR with one will alter the bit. that in in the IF part.

else the resultant number is odd , XOR with 1 will alter the bit.

- Sanjay Kumar February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would be lying if i said i really did understand that.. What resources can i use to learn bit manipulation in full? I mean books or online tuts

- llmorex February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ke solution is more perfect, I should have realized in both the cases XOR will work. no need to check even and odd..

- Sanjay Kumar February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One lil hint I can give about shifting left and right.. rest you can search on google.. I learned like that only..

in numbers Right shift by 3 ( n >> 3) mean dividing n by 2*2*2
in binary if n = 00100011 n>>3= 00000100

in numbers left shift by 3 ( n << 3) mean multiplying n by 2*2*2.
in binary if n = 00011000 n<<3= 11000000

- Sanjay Kumar February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int convert(int n , int m)
{
int i=0 ;
int num = n;
while( num>0)
{
num = num/2 ;
i++;
}
int test = pow(2,m-1);
if ((n|test)>n)return (n|test);
else return (n-test);
}

- popoff February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What does that mean flip?
The code shows bit-wise xor. I hate the question that are in-explicit and fuzzy.

- MiKL~ February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If its 1 change it to 0. Thats what the interviewer meant.

- llmorex February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If its 1 change it to 0. Thats what the interviewer meant.

- llmorex February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int temp=1<<(n-1)-1<<(n-2); // mask with only nth position being 1
return (temp^m);

- T-racer February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

m ^ (int)Math.pow(2, n-1) : Do XOR operation on the 2 to the power n will do the job.

- naves February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

- Administrator February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Funtion flip(m, n){
Return m^=1<<n
}

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

should be m ^=1<<(n-1)

- hello world February 09, 2012 | Flag


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