## Yahoo Interview Question for Software Engineer / Developers

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

I think the best is the XOR implementation, reason being:

when u use temp variable memory is used more and you access the register every now and then, when you are doing arithmetic operation you incure the overhead of ALU (hardware circuit) but bitwise operation is manipulation of bits at hardware level as the bits are already in the register which is fastest of three.

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

XOR operation can be used to swap any two variables with as long as they have same data types.Here are the steps for swapping a and b of same data types:
1> a = a^b
2> b = a^b
3> a = a^b
Read:- en.wikipedia.org/wiki/XOR_swap_algorithm

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

The method of Temporary variable can be used for swapping any kind of variables while the Arithmetic operation method can be used only for numbers. Also, there is a chance of overflow.
XOR is good for swapping integers but cannot be used for any other variables.

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

if the values are negavive then we can't use the XOR operator .

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

That's not correct, XOR swap works fine with signed and unsigned integers. The only caveat is that XOR swap fails with memory aliased values. So you need to check that the memory addresses are not equal.

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

I cannot see of XOR can be used to swap two variables w/o using temporary variable. Let's say swap(&a,&b); b = a^b^b then how about a after b has changed?

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

Draw of XOR:
When we have to swap float values it fails

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

No, XOR only cares about bits. As long as they're the same size it doesn't matter - cast as needed.

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

a) int temp=a;
a=b;
b=temp;

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

c) a^=b^=a^=b

The first method can be used even with user defined type however, it takes a temp variable.
The second method doesn't work with user defined types as arithmetic is not possible in case of user defined types.. However, after overloading arithmetic operator[like in C++], it is possible.
The third method is the best as it works on the bits at H/W level & thus doesn't require any temporary register. It thus, guarantees faster operation. However, it fails when the situation needs swapping user defined types.

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

Better on which criteria. First option is more readable and maintainable. And some performance gain by 3rd option (assumed) will it matter for modern compilers and modern CPU. I think 1st option is better, 2nd and 3rd option are less readable and just premature optimization

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

Actually, XOR requires 3 times of xor operations and 3 times of assignment.
Using tmp requires 3 times of assignment.
Of course it uses a variable but normally it is 4 bytes even it is integer or pointer. (or 8 bytes)
XOR normally doesn't support many types except int (or ambiguous, depending on language)
Using tmp is easier to make template inline function.
And much more readable.
So I prefer Using tmp than Using XOR.

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