A9 Interview Question for Software Engineer / Developers


Team: Search
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 5 vote

This link has code which counts it for both positive and negative numbers.

onestopinterviewprep.blogspot.com/2014/03/count-1s-in-binary-format-of-number.html

Enjoy :)

- codechamp March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes
Why are you upvoting your stupid threads with other accounts? praveen up-voted codechamp's comment: onestopinterviewprep.blogspot.com/2014 ... praveen up-voted codechamp's question: Given a matrix with ... tunguyen161088 said {{{public void Move(int[] a,int pos) { int current = pos ... praveen up-voted codechamp's question: COUNT 1s in BINARY ... .... - Anonymous March 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yes. Sockpuppets just show how low you are.

- Anonymous March 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{ public static int getNumOnes(int val)
{
int count = 0;
if(val<=0)
return count;
else
{
int mask=0X0001;

while(val!=0)
{
int c = mask & val;
{
if(c == 1)
count++;
val=val>>>1;
}

}
}
return count;
}}

- MB March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int num=sc.nextInt();
		int count_SetBit=countSetBit(num);
		System.out.println(count_SetBit);
	}
	public static int countSetBit(int num)
	{
		int count=0;
		int mask=0x0001;
		while(Math.abs(num)>0)
		{
			if((num&mask)==1)
				count++;
			num>>>=1;
		}
		return count;
	}

}
This Solution works for negative and positive number both.

- neelabhsingh April 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

main()
{
int n;
scanf("%d",&n);
num_of_1s(n);
}
void num_of_1s(int n)
{
static int count =0;
if(n)
count++;
esle
printf(count);
}

- krish March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int num=10;
int count=0;
while(num)
{
 count +=num%2 ==0? 0:1;
 num = num>>1;
}
cout<<count;
return 0;
}

- kirankumarcelestial April 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# using Math.Pow function. Not as sexy as the other solutions, but it works :)

protected int NumberOfOnes(int anInt)
    {
        int retVal = 0;
        
        int pow = 0;
        while (Math.Pow(2,pow) <= anInt)
            pow++;

        while (pow >= 0) {
            if (anInt >= Math.Pow(2,pow)) {
                anInt -= (int)Math.Pow(2,pow);
                retVal++;
            }
            pow--;
        }
        return retVal;
    }

- johny418 April 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To resolve the negative integer issue, cast to unsigned.

unsigned int count1s(int number) {
	unsigned int unumber = (unsigned int) number;
	unsigned int count = 0;
	while(unumber) {
		count += 1 ? (unumber & 1) : 0;
		unumber = unumber >> 1;
	};
	return count;
}

- Ehsan April 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

run like ./prog.rb 0 1 234 234234234

ARGV.each.map(&:to_i).each.inject(0) {|count, n| (((count += (n & 1)) && (n = n >> 1) while n != 0) || (puts count)) ? 0 : 0}

outputs:

0
1
5
16

- Anonymous April 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Golfscript, run like this echo "1 2 3 365 2 1" | golfscript [scriptname]:

[~]{{0=!}{2/}/}%{{1&}%{+}*}%`

- Anonymous April 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int onesInBinary(int n)
{
    int total = 0;
    while(n>0)
    {
        total += n%2;
        n /= 2;
    }

    return total;
}

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

def count1s(self, num):
        x = [ c for c in bin(num) if c == '1']
        return len(x)

- vivian April 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

private int count(int val) {
		int ret = 0;
		while(val > 0) {
			val&=(val-1);
			ret++;
		}

		return ret;
	}

- Darkhan.Imangaliev March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this actually works for positive numbers. negative numbers's 1s can be cumbersome to calculate though..

- kavitha March 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, you are right. depending on whether number int or long, we can do the following then:

int count = 0;
for(int i=0;i<len;i++){//where len =  32 if int, 64 otherwise
  if ((n & (1 << i)) > 0){
    count++;
  }
}
count+=(n >> (len-1)) & 1;

- Darkhan.Imangaliev March 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution does not even scale for positive numbers.

Lets consider val = 1001
The result from tour code returns 7, but the actual result is 6.

- kirankumarcelestial April 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Kiran, not sure about your scaling comment or not, but 1001 should return 7. 512+256+128+64+32+8+1 = 1001.

- johny418 April 01, 2014 | 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