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?

Joris Ooms
  • 247
  • 2
  • 8
  • 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? – RoflcoptrException Mar 21 '11 at 11:34
  • Oops, I forgot to mention that :'D Edited. Thanks. – Joris Ooms Mar 21 '11 at 11:35
  • 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 '11 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 '11 at 11:47
  • 2
    Anyone who gives that answer better be prepared to immediately follow it up by warning about the potential integer overflow it could cause. – Carson63000 Mar 21 '11 at 11:48
  • Your solution wouldn't work if the values where string literals. There is an xor version that works very will, with all data. – Mumbles Mar 21 '11 at 11:51
  • 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. – Joel Etherton Mar 21 '11 at 11:55
  • Thanks everyone for the comments. I guess I'll take up a discussion with my teacher about this and see how he reacts/defends the riddle. Anyway, are there sites or books with 'real' riddles/puzzles related to programming? Thanks. – Joris Ooms Mar 21 '11 at 11:56
  • As for this being general across programming languages, c++ std lib has swap(a,b) which does exactly that, and anything with tuple unpacking (python, perl, tr1) can do (a,b) = (b,a). – Phil H Mar 21 '11 at 12:13
  • @Joris: I do not think there are real riddles/puzzles like what you ask. It is **not** a programming puzzle in the first place. This question, at best, tests whether you have a brain beneath the skull and if that is functioning. That is all. – Kanini Mar 21 '11 at 12:29
  • Okay, thanks. I just found out about 'The C Puzzle Book'. Guess I'll check that one out and do some puzzling. – Joris Ooms Mar 21 '11 at 12:35
  • 1
    at first it can look like this task is about thinking out of the box, but it very wrong. As mention before it _not_ programming task and it try to learn you to programming with "magic" and this is worst part. You never ever ever will do that in real job. – Dainius Mar 21 '11 at 12:35
  • 4
    `list($a, $b) = array($b, $a);` – Htbaa Mar 21 '11 at 13:04
  • Are you allowed to hack the symbol table? That could be another approach. – David Thornley Apr 15 '11 at 14:45
  • What does the responsibilities of this job include? – CMR Apr 15 '11 at 17:51
  • Doing such a trick might make sense if you're dealing with an embedded system with very limited RAM. Other than such special cases such tricks have no business in good code. – Loren Pechtel Jan 25 '12 at 19:51
  • @LorenPechtel No, not even there. Chances are, the compiler will optimize a swap through a temporary to some kind of xchg instruction without any temporary. It probably won't for an arithmetic or bitwise trick. – Sebastian Redl Apr 17 '15 at 09:38
  • 1
    The only reason I would ever ask such a question in an interview is if I had a team of very opinionated developers and needed another senior programmer capable of standing up to them. The correct interview response would be for the candidate to stand up to me. And this is a very risky proposition. – Sebastian Redl Apr 17 '15 at 09:39
  • @SebastianRedl if an interviewer were to ask me that question I'd wonder why he asked it but not voice my opinion about where he should stuff it, a doing so is more likely to get you rejected than accepted (even if in your specific case it'd be exactly what you are looking for). In fact I've at least once rejected an offer to do a "test" that had questions like that, because if that's the kind of questions they ask during a programming test it's unlikely to be the kind of environment I'd want to work in. – jwenting Apr 17 '15 at 09:41
  • @jwenting As I said, very specific circumstances, and looking for someone with a very special mindset. – Sebastian Redl Apr 17 '15 at 09:42
  • @SebastianRedl You're assuming there is an xchg instruction in the underlying machine code. While I've never done embedded systems work my impression is that the languages are pretty simple. – Loren Pechtel Apr 17 '15 at 22:17

5 Answers5

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.

Rein Henrichs
  • 13,112
  • 42
  • 66
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.

KeesDijk
  • 8,918
  • 4
  • 35
  • 41
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.

Jonathan
  • 1,839
  • 2
  • 12
  • 11
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).

Jaster
  • 153
  • 3
  • 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. – Karl Bielefeldt Mar 21 '11 at 13:10
  • 1
    @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
  • You're right, I don't understand your answer. What exactly about this problem, besides the syntax, makes it not work on other languages? – Karl Bielefeldt Mar 21 '11 at 16:28
  • Considering functional languages avoid side effects, I think this solution wouldn't work in any of them. – anthonyvd Apr 15 '11 at 13:06
  • @Karl Bielefeldt: Sorry, it doesn't work on any language supporting signed integers. In C and C++, integer overflow results in undefined behavior. It happens to work on all modern implementations I personally know of, but that's not the same thing. It is guaranteed to work on C and C++ unsigned integers. – David Thornley Apr 15 '11 at 14:44
  • 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. – Rein Henrichs Apr 15 '11 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 '12 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.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • 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 '15 at 21:22