Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm not entirely sure if this belongs here, so feel free to move/close it, if necessary.

The other day, in our PHP class, our teacher gave us a challenge used by a friend of his in job interviews. It works in every programming language, so it's not limited to PHP. He said that his friend uses this 'riddle' to weed out the people who can't think of a fast answer when it comes to logical challenges. The people that don't solve it won't get a job, of course.

The riddle is as follows:

    $a = 3;
    $b = 7;

    echo "a = $a";        // has to become 7
    echo "<br />";
    echo "b = $b";        // has to become 3

You basically have to switch the contents of both variables without doing lame things like $b = $a + 4. You cannot use a temporary variable either! I struggled with this, I have to admit that; I was like 'oooh yeah' when we finally got the answer.

I don't want to spoil this for anyone, so instead of posting the solution I'll just put a link.

Now, as for my question. I was wondering if there are more riddles like these out there, that people (that's you, SO) use in job interviews, etc. Perhaps even a bit harder than this one. My goal is to train my logical thinking a bit and get better in solving issues like this. Perhaps there are any books or websites out there devoted to stuff like this?

share|improve this question

migrated from stackoverflow.com Mar 21 '11 at 11:41

This question came from our site for professional and enthusiast programmers.

Hmm I dont see the riddle here? Or aren't you allowed to use a temporary variable to hold one of the both values? –  RoflcoptrException Mar 21 '11 at 11:34
Dumbest f*ing challenge ever. I detest interview questions like this. They're a sign that the people you want to work for are lazy and dull. When, ever, will you have to switch two integer variables without using a third, temporary, variable? If anybody did this kind of trick in a production environment I would smack them in the head with a rubber mallet. Of all the things I'd want in a new employee, knowing some stupid lame ass mathematical trick you can learn in five seconds by googling is the LAST thing I'd ever want. Ask me to do this in an interview and I'd tell you FYNQ. –  Will Mar 21 '11 at 11:45
+1 on Will's comment. I call that a "cute" solution. It's clever and it works, but it's difficult to decipher for such a simple operation, won't get optimized by most compilers and only works for numeric types. (In some situations with IEEE floats and doubles, it can produce incorrect results because of representational errors you encounter with those types.) My answer would be to ask the interviewer what benefit a cute solution has over a bog-standard, temporary-variable swap and why he thinks that's an appropriate thing to do in production code. –  Blrfl Mar 21 '11 at 11:47
Is this a programming job or a mathematics job? That solution doesn't employ any programming techniques so there's nothing unique to any language or special programming knowledge. As much as you may need a job, you probably don't need THIS job. –  Joel Etherton Mar 21 '11 at 11:55
list($a, $b) = array($b, $a); –  Htbaa Mar 21 '11 at 13:04

4 Answers 4

up vote 5 down vote accepted

First I totally agree with the comment from Will.

Second to answer your question look at this site for similar questions

Also the following two programmers questions can give you enough to study.

share|improve this answer

You could also do this with an XOR swap:

$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;

Technically it might be faster than the addition/subtraction, but really it's splitting hairs.

share|improve this answer

This isn't a "riddle", it's just an obsolete algorithm (the XOR swap algorithm).

The problem with the question (and others like it) is that the only thing it can tell an interviewer is whether or not the programmer has happened to memorize an odd bit of programming trivia. As an interviewer, I choose not to waste my or the interviewee's time with such nonsense. There are far more important things to do with that time.

share|improve this answer

Actually this does NOT work on any langues. It works only for a very limited number of languages. So this is just a PHP related thing (can't actually think of any other language where this works).

To answer your questsion: There are many "riddles" like this, but the point about interviews is not always solving the riddle. If you can explain your thoughts right you could still get the job (also if your given answer isn't correct).

However I have never encoutered a riddle twice, so my guess is that every company creates their own puzzles that suits their needs (including language, etc).

share|improve this answer
I don't think you understand the question. The php syntax is irrelevant. The solution works on any language that supports signed integers, which I feel pretty safe in saying is all of them. –  Karl Bielefeldt Mar 21 '11 at 13:10
@Karl, it has to support named variables too. Goodbye to a small but non-zero set of functional languages. –  Peter Taylor Mar 21 '11 at 13:34
@Karl I don't think you understand my answer. –  Jaster Mar 21 '11 at 13:49
@Jaster There is no reason why this couldn't be implemented in any language... unless you're seeing something the rest of us aren't.... –  Kenneth Mar 21 '11 at 14:10
It doesn't work in "a very limited number of languages." It works in almost every language that supports variable reassignment and an XOR operator. –  Rein Henrichs Apr 15 '11 at 15:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.