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;

- Mir February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Shekar Gurram February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- andykumar116 February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Shekar Gurram February 03, 2012 | Flag
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;

- Anonymous February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Shekar Gurram February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anony February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Abhi February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Abhi February 03, 2012 | Flag
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;

- D February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Abhi February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Abhi February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- kothabhaskar February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should note it only works for integral types.

- Biff February 03, 2012 | Flag Reply
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

- dave February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops, I mean ^

- dave February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Chitra July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Chitra July 01, 2015 | 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