Hacker News new | ask | show | jobs
by Deestan 1285 days ago
Too many of them are in effect a long-winded quiz for a memorization task.

How would you swap two integers without using a temporary variable? Seen it before? Pass. Not seen it before? Fail.

2 comments

What irks me about this question is that it is almost utterly unrelated to any type of development task, save one: pipelining a repeated math equation in a long loop. And even then, I would bet the time it takes to swap two registers (or even L1 cache) using a third would far outperform the three operations needed because of the fact that they are sequential in nature (ie: they cannot be performed simultaneously by even an advanced CPU).

In nearly 30 years of diverse coding experience, I've never once encountered a situation where this solution would be useful.

You can absolutely puzzle out _a_ solution to that problem, it's not just a quiz. It's not even hard, given the context that you know that it's possible to do.

Steps, mostly driven by just basically knowing the goal and that there's not many operations that could possibly help:

"a" "b"

"a+b" "b"

"a+b" "-a"

"b" "-a"

"b" "a"

Then once you have that, you can enumerate the downsides to that, look for more efficient and less error-prone ways to proceed.

I'm sorry. That one uses a hidden temp storage for the intermediate calculations.

We were looking for the solution based on XOR which works with only two registers.

We wish you luck with your job search elsewhere.

That's not what someone giving even this poor interview question would answer with. It's also not true.

These interview questions are fairly shit, but they're not _that_ shit, and the people giving them are doing their best.