DEV Community

Discussion on: Terrible interview question: Swap variables without a temporary

Collapse
 
lazarljubenovic profile image
Lazar Ljubenović

Yeah, this is one of the standard answers for numerical data. You can do similar things with multiplication/division (but you'll have issues there if one of the variables holds a zero). In low-level languages like C you can also do it with pointers, since they are usually just integers (memory locations), so that's a bonus too. The algorithm with XOR, however, works on every datatype since it works with bits.

If you get asked the question, you should by all means state both answers and their pros and cons! Solving problems never has a unique solution, and being able to rationalize about and understand the differences between approaches (and the constraints they impose on the problem, if they exist) is the key part to developing the necessary skills for solving problems you've never seen before -- and there's a high demand for these people everywhere.

Collapse
 
ytjchan profile image
ytjchan

With pointers, does overflow affect this kind of swapping?

Thread Thread
 
lazarljubenovic profile image
Lazar Ljubenović

It depends on the language, but when talking about bit-level algorithms and pointers, people usually have C/C++ in mind. It will work properly if you use unsigned integers in Standard C, because adding and subtracting (unsigned) integers is defined by assuming modular arithmetic.

If you leave the default int type, which is a signed integer, a compiler might optimize something away and break the formula in case the overflow occurs.

Thread Thread
 
itr13 profile image
Mikael Klages

No, because (a+b)%c equals ((a%c)+(b%c))%c

Same with subtracting.

 
nikodannemiller profile image
Niko Dannemiller

I don't have much experience with pointers but from what I've played around with it might. I think it will have to do with your compiler and how much memory is available.

Thread Thread
 
nikodannemiller profile image
Niko Dannemiller

That's a good point. I've assumed that it will just roll over cleanly into negatives or roll down into positive (if both numbers are large negatives) but you're right some compilers in that case.

Thread Thread
 
lazarljubenovic profile image
Lazar Ljubenović

Could be. It's been a reeeeally long time since I've written anything in C or Assembly, so can't speak from the first hand at the moment, unfortunately.

Thread Thread
 
nikodannemiller profile image
Niko Dannemiller

S'all good. I'm still in Uni so outside of little side projects I only really know what professors have brought up. Might ask one about this problem.

Collapse
 
nikodannemiller profile image
Niko Dannemiller

Oh heck you're right on the pointer front I hadn't thought of that thanks.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

You can't do it with every data type, unless you write in assembler and directly access registers. High-level language virtually all say this is undefined behaviour. You can't treat pointers, or any other type as an unsigned 2's complement integer -- the type where this xor'ing works. It's either unspecified, or even undefined behaviour.

Thread Thread
 
nikodannemiller profile image
Niko Dannemiller

That's fair. My personal experience is a bit limited so it's always good reading articles like this.