3

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?

21
  • 2
    Hmm I dont see the riddle here? Or aren't you allowed to use a temporary variable to hold one of the both values? Mar 21, 2011 at 11:34
  • 82
    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.
    – Ripped Off
    Mar 21, 2011 at 11:45
  • 10
    +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, 2011 at 11:47
  • 5
    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. Mar 21, 2011 at 11:55
  • 4
    list($a, $b) = array($b, $a);
    – Htbaa
    Mar 21, 2011 at 13:04

5 Answers 5

5

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.

9

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.

4

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.

3

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).

9
  • 5
    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. Mar 21, 2011 at 13:10
  • 1
    @Karl, it has to support named variables too. Goodbye to a small but non-zero set of functional languages. Mar 21, 2011 at 13:34
  • @Karl I don't think you understand my answer.
    – Jaster
    Mar 21, 2011 at 13:49
  • 1
    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. Apr 15, 2011 at 15:34
  • 1
    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, 2012 at 14:58
1

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.

1
  • 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, 2015 at 21:22

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