employer cover photo
employer logo
employer logo

Northwest Territorial Mint

Questa è la tua azienda?

Domanda di colloquio di Northwest Territorial Mint

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

Risposta di colloquio

Anonimo

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.