Qualcomm Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
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.
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.
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 :)
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;
}
'bits set' in an integer usually means counting 1's. So yes , count only the number of 1's in the integer
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!
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.
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);
}
//Write a program to find the middle element in a linked list
static void find_middle_element(struct node* head_pointer)
{
int count = 0x0;
int i = 0x1;
struct node* ptr = NULL;
ptr = head_pointer;
//get the length of linked list
count = get_length_of_the_list(head_pointer);
if(count & 0x1) //odd linked list
{
while(i != (count+1)/2)
{
ptr=ptr->nxt;
i++;
}
printf(" \n middle element is = %d",ptr->data);
}
else //even linked list
{
while(i != count/2)
{
ptr = ptr->nxt;
i++;
}
printf("\n Middle elements are = ");
printf("%d -->",ptr->data);
ptr = ptr->nxt;
printf("%d",ptr->data);
}
}
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));
x->next = LList->Head
for (int i = 0; i <= (length-1); i++)
{
x->next = LList->next;
}
printf("%d", x -> data);
To swap two numbers w/o using a temp variable, use XOR (^):
- Anonymous February 27, 2012a = a ^ b
b = a ^ b
a = a ^ b
Try it with this and see for yourself:
a = 0011
b = 0101