Interview Question

Technology Developer and Analyst Program Interview New York, NY

Swap two numbers while not using the temp variable.

Answer

Interview Answer

3 Answers

2

using the XOR operator

Interview Candidate on Mar 10, 2013
5

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

Malavika on Sep 26, 2013
0

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

This will avoid over flows which might come in addition steps

Anonymous on Feb 4, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.