Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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

closed as too broad by MichaelT, GlenH7, Ixrec, TZHX, Dan Pichelman Apr 22 at 20:45

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

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

5 Answers 5

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

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

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

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
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
I'd say, that the question makes sense in limited number of languages. In some you'll just say (b,a) = (a,b) –  vartec Jan 25 '12 at 14:58

I wonder what Joris' teacher's response was.

The biggest problem with this is that the trick is obscure and anyone who used it in a professional programming environment is really adding difficult to maintain code. This is a huge problem for a professional place, anywhere that has very large codebases needs easy-to-understand code so future workers can see what its doing without spending large amounts of time grokking code, or worse - not understanding it and making a mistake.

If you need an example, look at the Story of Mel. Very fancy code, neat trick, useless professional programmer. You might say the only reason this is acceptable is that it was a very tight environment where such tricks made a difference. In 99.999% of modern environments this would be marked as requiring rework.

share|improve this answer
Hi, since this was over 2 years ago, I don't remember my teacher's response. I have never needed to solve any such silly questions during job interviews and I would definitely tell an interviewer 'FYNQ' as @Will would do. In the end, I'm not a computer scientist but merely a (mostly) self-taught web/rails developer and I haven't dug into things like Project Euler or similar. Perhaps I should, someday, but I'm fairly sure I lack the mathematical skills and/or knowledge of algorithms and basic computer science. At this point in my career, it's not my primary concern either. I get by :) –  Joris Ooms Apr 19 at 21:22

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