Qualcomm Interview Question for Software Engineer / Developers






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

int i=0;
while (n){
n=n&(n-1);
i++;}
return i;

Thanks!

- Naga Samrat Chowdary, Narla May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think there is some thing very wrong with your code, coz your bitwise anding a no with it's previous no.

Eg if i've to check no of 1's in n=8 then according to your logic n=n&(n-1) would give 0 i would become 1. Then after that control would exit the loop, because of condition while(n) as n would get changed to a ZERO.

Try to alter the code or check each bit.

- somename May 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think naga's code works just fine...@someone...if you take the example of 8 then you are right after the first iteration the number becomes 0 and that is the desired outcome because 8 will contain only one "1".

- puneet May 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes u are right, i didn't pay attention to the increment operation.
sorry man! naga

- someone May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if n is negative then?

- se August 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will work for positve and negative also.

- Anonymous October 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is something that works with both negative and positive numbers --

int number_of_bits(int input)
{
    int nAnder = 1;
    int nCount = 0;
    while( nAnder )
    {
         if( input & nAnder )
              nCount++; 
         nAnder = nAnder << 1; 
    } 
    return nCount; 
}

- Girish September 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you are using "<<" then nAnder should be set to 1<<31 instead of 1. right?

- Anonymous November 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I feel like there might be a way to add the binary digits ie. for 7 => 0111, 0+1+1+1

numberOfOnes(7){
digitArray[] = split(toBinary(7));
for(int i = 0; i < sizeOf(digitArray); i++){
result += digitArray[i];
return result;
}
}

- jag August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess this also just works fine...
#include<iostream>
#include<stdio.h>

int main()
{
int n;
printf("\n Enter the number : ");
scanf("%d",&n);
int x=0;
// main logic
while(n!=0)
{
if(n%2!=0)
x++;
n=n/2;
}

printf("\n no. of binary one's is : %d \n",x);

return 0;
}

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

int numberOf1s(int n)
{
   int count = 0;
   if(n==0)return  count;
   while(n&=n-1)count++;
   return ++count;
}

void main()
{
  printf("%d",numberOf1s(7));
}

Output: 3
Works even for negative numbers, tried it!

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

good one.. it works

- sai February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Look-up Table to improve the performance.

- Ian F. April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countOnes(unsigned int num){
	int count = 0;
	while(num){
		num &= (num-1);
		count++;
	}
	return count;
}

- swapnilsj August 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the easy approaches of the above algorithm is to check when the & operation of the given number with 1 is 1 because at that case only 1 bit will be the number where this condition is satisfied and elsewhere it will be 0 so, at the former case increment the counter variable and at the end return the count

Implementation:

#include<bits/stdc++.h>
using namespace std;
int countbits(int n){
int count = 0;
if(n == 0)
return 0;
while(n){
if(n&1 == 1)
count++;
n = n>>1;
}
return count;

}

- swapnilkant11 June 18, 2019 | 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