employer cover photo
employer logo
employer logo

Northwest Territorial Mint

Ist dies Ihr Unternehmen?

Frage im Vorstellungsgespräch bei Northwest Territorial Mint

How do you swap two numeric values in two variables without using a third temporary variable?

Antwort im Vorstellungsgespräch

Anonym

7. Apr. 2014

My solution was to use exclusive-or operations: x = x ^ y y = x ^ y x = x ^ y The interviewer liked the solution, but then suggested another solution using addition and claimed that it would also work with floating point numbers: x = x + y y = x - y x = x - y However, I brought up that this solution has problems since you have the risk of underflow/overflow, and in the case of floating point numbers, you risk loss-of-significance errors. The interviewer did not offer to discuss.