Qualcomm Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
8
of 8 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 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 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 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 February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 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 October 11, 2012 | Flag Reply
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 May 13, 2012 | 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 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 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 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 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 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 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 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 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 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 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 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 May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 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 August 07, 2013 | Flag Reply
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 
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);
    }
}

- Shreyas Ravindra June 29, 2014 | Flag Reply
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));
x->next = LList->Head
for (int i = 0; i <= (length-1); i++)
{
x->next = LList->next;
}
printf("%d", x -> data);

- Priyank Shah September 14, 2014 | 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