Qualcomm Interview Question Software Engineer in Tests


Country: United States
Interview Type: In-Person


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

To swap two numbers w/o using a temp variable, use XOR (^):

a = a ^ b
b = a ^ b
a = a ^ b

Try it with this and see for yourself:

a = 0011
b = 0101

- Anonymous on February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 vote

Q2.)
let two numbers are a and b, they can be swapped like below
a = a + b
b = a - b
a = a - b

Q3.)
Maintain two pointers slow and fast.
Iterate thru the list by moving slow pointer once and fast pointer twice. When fast pointer will point to null, slow pointer will be pointing to middle element.

- Code-Warrior on February 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

answer to Q2 is wrong.
Step 1 : a = a + b
This step may result into an overflow which is not supported.

Remember, you are not writing a mathematical equation, it is an ALGO.

- Prabirhbhatt@gmail.com on February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Q2.
I think this is almost right...
Line:
b = a - b
will implicitly perform an operation in another variable. E.g.:
c = a - b
b = c
To make it independent of any temporaries, have the variable that is set, be the left operator on the right side of the equation.

a = a + b // or a += b
b = -b
b = b + a // or b += a
a = a - b // or a -= b

This is what it would look like in pseudo-assembly language:

add a, b
neg b
add b, a
sub a, b

No temps used anywhere :)

- Anonymous on February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

to get number of bits:
divide by 2 until u get a 0 using a while loop. increment a counter in each iteration.

- suraj on May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Count number of bits set

unsigned int countOnes(unsigned int i)
{
   unsigned int c;
   for (c=0; i; c++)
       i &= i-1;
   return c;
}

- sraj on October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

what about below code to get bits in an integer

#include <stdio.h>

int main()
{
	int mask = 1;
	int i ;
	int cnt = 0, tmpCnt = 0;
	int num;
	
	printf ("Enter number \n");
	scanf ("%d",&num);
	
         num = 31;

	for (i = 0; i< (sizeof (int)*8) ; i++)
	{
                  //printf ("checking %d bit mask is %d\n",i,mask);
                  //printf ("result %d & %d = %d\n",num,mask, (num&mask));
		if ((num & mask) == mask)
		{
			cnt ++;
			cnt = tmpCnt + cnt;
			tmpCnt = 0;
		}
		else
			tmpCnt ++;
		
		mask = mask << 1;
	}
	
	printf ("Number of Bits in a Number %d\n", cnt);
}

- ravneetsingh1986 on August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Q1)

int count_bits(int n)
{

    int x = 1;
    int res = 0;

    while(x < n)
    {
        if( n & x != 0 )
            res++;

        x = x << 1;
    }

    return res;
}

- Hitman on February 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its counting only number of 1's in bits(what about zero's)

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

'bits set' in an integer usually means counting 1's. So yes , count only the number of 1's in the integer

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

The question is to count the number of bits in an integer, not the number of bits set.
can't we simply do a sizeof() and multiply it by 8?

though personally i believe if the question was to count the number of set bits, it wud make more sense!

- Sougata Chatterjee on May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think the if statement is not necessary, and you have to say "while (x <= n) ", else the above code will fail for numbers whose MSB is 1 and the rest bits are 0.

- solx on July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

counting number of bits program is wrong........this program counts only number of 1's in the bits. (what about zero's bit)

- kk on March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* find the mid of the list */
for (i=0,j=0;i<n;i++)
{
if (i/2 > j) j++;
printf("i=%d, j=%d\n",i,j);
}

- Anonymous on May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count_bits(int n)
{
int x = 1;
int res = 0;
while(x < n)
{
if( n & x != 0 )
res++;
x = x << 1;
}

return res;
}

if you think this gives only 1 the ans is

int count_bits(int n)
{

return (sizeof(int)*8)
}

return res;
}

- Thirumal on June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
int n=1,c=0;
while(n!=0)
{
n=n<<1;
c++;
}
return c;
}

- nehal on July 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i = 0;
do{
num = num/2;
remain[i] = num%2;
i++;
}while(num!=0)
print(i); // i = count of bits

- victor on November 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take 2 pointers x1 and x2.
Keep x1 at the starting point of link list and x2 to start->next.
Increament x1 by 1 and x2 by 2 till reach end of list.
x1->num is the answer.

- victor on November 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think both shud start together. Say for 11 nodes:
x1 : 1,2,3,4,5,6
x2 : 1,3,5,7,9,11

return : 6 (whuch is the middle node)

- Sougata Chatterjee on May 24, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through 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