Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

better

If you subtract -1 from a given number it will convert the last "1" bit to "0" followed by continuous tail of 1, remove 1's tail by taking "&"

int countBits(int n)
{
        int result = 0, temp = 0;
        while( n != 0)
        {
                result++;
                temp = n--;
                n = n&temp;
         }
        return result;
}

- Wayne March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

superb :)

- chandan prakash March 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Make use of shift operator.

- King@Work March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int count(int n)
{
int c = 0;
while(n>0)
{
if(n | 1 == 1) // ORing the number with 1 gives 1 if the right most bit is '1'
{
c++;
}

n = n >> 1;
}



return c;
}

- hitman March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

shouldn't you AND it with 1. ORing with 1 will always yield 1 even if the rightmost bit is 0.

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

You must not OR it as the result will always be TRUE or "1".

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

OR it with 0 will give you 1 if the last bit is 1

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

int x; // number (32 bits)
x = (x & 0x55555555) + ((x & 0xaaaaaaaa)>>1); // 0..2 ones in 2 bits
x = (x & 0x33333333) + ((x & 0xcccccccc)>>2); // 0..4 ones in 4 bits
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);   // 0..8 ones in 8 bits
x = x  + (x >> 8);   // 0..16 ones in 8 bits
x = x + (x >> 16); // 0..32 ones in 8 bits
x &= 0xff;

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

This is the fastest but the worst response you could give in an interview. How do you explain or deduce this in an interview? Apart from "I searched for popcount on google and memorized this"....

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

well they certainly do not expect you to invent a bike on the interview)) but you can explain the logic behind it to make sure you understand - then you don''t need to memorize it "by hearth"

the logic is in fact pretty easy:
in the first step we count ones in each group of two bits
this is done by ((x & 1010101...) >> 1) + (x & 01010101...)|

then we repeat the same op but now for each group of 4 bits
with the masks 110011001100... and 001100110011.. resp.

btw once you get this algorithm you can apply a similar idea to other bit manipulation e.g. bit reversal or finding the highest bit set..

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

Wow. This is really nice and very easy to understand too !

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

Its IBM US Patent 6516330, algorithm.

- rajeshrp21 April 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think it will be in O(log n) using this code

int count(int n)
{
   int c = 0;
   while(n>0)
   {
      if(n%2 == 1)
      {
         c++;
      }

      n/=2;

   }

   return c;
}

- hitman March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is same as bit shifting algo (with AND instead of OR).
instead of a right shift arithmetic operation is done here.

- Anonymous April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

better

If you subtract -1 from a given number it will convert the last "1" bit to "0" followed by continuous tail of 1, remove 1's tail by taking "&"

int countBits(int n)
{
        int result = 0, temp = 0;
        while( n != 0)
        {
                result++;
                temp = n--;
                n = n&temp;
         }
        return result;
}

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

int x; // number (32 bits)
x = (x & 0x55555555) + ((x & 0xaaaaaaaa)>>1); // 0..2 ones in 2 bits
x = (x & 0x33333333) + ((x & 0xcccccccc)>>2); // 0..4 ones in 4 bits
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);   // 0..8 ones in 8 bits
x = x  + (x >> 8);   // 0..16 ones in 8 bits
x = x + (x >> 16); // 0..32 ones in 8 bits
x &= 0xff;

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

Kerninghan's algorithm

int countsetbits(int n)
{
int count = 0;

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

return count;
}

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

public int Count(int n){
	int count=0;
		while(n>0){
       		 	count+=n&1;
        		n=n>>1;
        	}
	return count;
}

- sriwantha March 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int res=0;
        while(n>0){
            if((n&1)==1)
               res++;
            n>>>=1;
        }
        System.out.println(res);

- priyamraj97 August 02, 2017 | 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