## EMC Interview Question for Developer Program Engineers

Country: United States

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

a = a^b;
b = a^b;
a = a^b;

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

This works perfectly fine but fails when a is equal to b.
Should use an if condition before using this logic to check for a = b.

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

@mail.qurram : dude this works even if a=b;
suppose a=b=4 (100) . i.e. a=a^b makes a=000 ,b=a^b makes b=100 then a=a^b i.e (000)^(100) makes a=100...

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

I read about this long back. Just remember very little, but found out the actual thing which I intended to focus on. Although a very very specific case, its worth knowing it.

where you have written your code like this

``````swap(int *a, int *b)
{
*a ^= *b ^= *a ^= *b;
}``````

Now, if suppose, by mistake, your code passes the two pointers to the same variable to this function. Guess what happens? Since Xor'ing an element with itself sets the variable to zero, this routine will end up setting the variable to zero (ideally it should have swapped the variable with itself). This scenario is quite possible in sorting algorithms which sometimes try to swap a variable with itself (maybe due to some small, but not so fatal coding error). One solution to this problem is to check if the numbers to be swapped are already equal to each other.

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

a = 5;
b = 6;
a = a + b;
b = a - b;
a = a - b;

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

This can fail in a case where the sum of a and b is greater than the integer max value.

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

This question is very well explained in (nobrainer.co.cc)

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

a=a+b-(b=a);

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

a=a+b-(b=a);

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

a=10;
b=5;
a=a+b;
b=a-b;
a=a-b;

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

a=a+b-(b=a);

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

a=a+b-(b=a);

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

#define swap(x,y) x^=y^=x^=y

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

a=a^b^(a=b);

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

Should note it only works for integral types.

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

a=a & b; // a = a XOR b
b=b & a; // = b XOR (a XOR b) = a
a=a & b; // = (a XOR b) XOR a = b

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

Oops, I mean ^

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

a=a+b;
b=a-b;
a=a-b;

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

a = a+b;
b = a-b;
a = a-b;

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

``````a = a+b;
b = a-b;
a = a-b;``````

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.

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