## Qualcomm Interview Question for 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

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.

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
0

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:

neg b
sub a, b

No temps used anywhere :)

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.

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;
}``````

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 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++)
{
{
cnt ++;
cnt = tmpCnt + cnt;
tmpCnt = 0;
}
else
tmpCnt ++;

}

printf ("Number of Bits in a Number %d\n", cnt);
}``````

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;
}``````

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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!

Comment hidden because of low score. Click to expand.
0

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.

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)

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);
}

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;
}

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;
}

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

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.

Comment hidden because of low score. Click to expand.
0

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)

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

``````//Write a program to find the middle element in a linked list
{
int count = 0x0;
int i = 0x1;
struct node* ptr = NULL;

//get the length of linked list

if(count & 0x1) //odd linked list
{
while(i != (count+1)/2)
{
ptr=ptr->nxt;
i++;
}
printf(" \n middle element is = %d",ptr->data);
}
{
while(i != count/2)
{
ptr = ptr->nxt;
i++;
}
printf("\n Middle elements are = ");
printf("%d -->",ptr->data);
ptr = ptr->nxt;
printf("%d",ptr->data);
}
}``````

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

1. /*If i is an integer, double, long or float.*/
Number of bits = sizeof(i)*8

2. a = a^b
b = b^a
a = a^b

3. /*gives length of linkedlist. Name is LList and lets say its even */
int length = sizeof(LList)/sizeof(LLNode)

LLNode *x = (LLNode *)malloc(sizeof(LLNode));
for (int i = 0; i <= (length-1); i++)
{
x->next = LList->next;
}
printf("%d", x -> data);

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.

### 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.