Bank of America Interview Question: Swap two numbers while not us... | Glassdoor

Interview Question

Technology Developer and Analyst Program Interview New York, NY

Swap two numbers while not using the temp variable.


Interview Answer

3 Answers


using the XOR operator

Interview Candidate on Mar 10, 2013

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

Malavika on Sep 26, 2013

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.