Qualcomm Interview Question for Software Engineer / Developers






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

if ( (n&(1<<i))^(n&(1<<j)) ){
n|~(1<<i);
n|~(1<<j);
}
return n

- Anonymous October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the solution above is incorrect,
because if the ith bit is 1, and jth bit is 0
and ith bit is more significant
then after n | ~(1<<i), and n | ~(1<<j)
ith bit is still 1

- Anonymous October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (( (n&(1<<i))>>i)^((n&(1<<j))>>j){

}
return n

- Anonymous October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (( (n&(1<<i))>>i)^((n&(1<<j))>>j){
n^=(1<<i);
n^=(1<<j);
}
return n

If two bits are different, we just need to flip those two bits.

- Anonymous October 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct!

- Taesung October 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct

- Rohan January 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should be (i-1) and (j-1).
Otherwise right.

- Hi January 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a ^= (1<<i);
a ^= (1<<j);

- Rajesh A December 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is'nt it true that in all the solutions above, if i and j are the given positions to be swapped, then we need to shift bits by i-1 and j-1 positions (eg: 1<<(i-1),etc)? correct me if i m wrong.

- Anonymous January 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That above is true if the position starts from 0 and not 1.

- Divz January 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if((n&(1<<i)) != (n&(1<<j)))
{
n = n^(1<<i);
n = n^(1<<j);
}

- walain November 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// swaps the ith bit and jth bit and returns swapped int
// assumes i=0 for the first bit from right
// and i-1 position for ith bit form right
int swap(const int &x, unsigned int i, unsigned int j)
{
// if the bits are same then return the same number
if ((int(1) & (x >> (i-1))) == ((int(1) & (x >> j-1))))
{
return x;
}
int ithBitSetToOne = (int(1) << (i-1));
int jthBitSetToOne = (int(1) << (j-1));
int bothIandJSetToOne = ithBitSetToOne | jthBitSetToOne;
return (x ^ bothIandJSetToOne)^int(0);
}

- Ashish November 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x = ((n >> i) ^ (n >> j)) & 1; // x is toggle flag for positions i and j
return n ^ ((x << i) | (x << j)); // apply toggle to bits i and j

- heavyarms1912 January 03, 2017 | 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