Qualcomm Interview Question Software Engineer in Tests

  • qualcomm-interview-questions
    0
    of 0 votes
    15
    Answers

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

    - Gloryhunter on February 26, 2012 in United States Report Duplicate | Flag
    Qualcomm Software Engineer in Test Algorithm

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

- Anonymous on February 27, 2012 | Flag Reply
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.

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


Add a Comment
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.

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