Qualcomm Interview Question Software Engineer in Tests

• 0

Count the bits in a Integer, Swap two numbers without using any temp variable, Find the middle element in the linked list

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
5
of 5 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.
3
of 3 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.
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
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.

Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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.