DEV Community

Discussion on: How can you swap two variables without using a third?

Collapse
 
moopet profile image
Ben Sinclair • Edited

Here's a warm-up interview question that I ask every candidate.

I've seen this question come up a lot, along with fizzbuzz or "write a sorting function" and I'm not sure it's useful anymore.

It's a purely academic problem that touches on several different concepts, none of which are going to be used by 99% of programmers these days.

Why do you ask this question? What do people's different answers tell you, or are you more looking for how people attempt to solve it (if they've not heard it before) or how quickly they regurgitate a stock solution (if they have?)

If I asked this, and someone answered quickly (they already know why the dead man in the desert is lying next to an unopened pack...) then I'd follow up with something asking when they had ever had to do this in real life, or even if they could come up with three examples of situations where it would be a useful thing to do in real life. If they couldn't, then I don't think I would have learned anything useful about them.

What do you get out of it?

Collapse
 
theoutlander profile image
Nick Karnik

Good question! I used to ask this as a warm-up question back in the days at the beginning of an interview. (However, I've also used it in recent interviews after this post). I've never used this question to influence the outcome of the interview. It's merely conversational and a segue into academics to pique their interest into a deeper technical conversation.

If the candidate has heard the question before, I will follow up with a question around if the solution handles overflow when numbers are large and also dive a bit into perf. We may dive into when and if this solution might be needed for specific use-cases. The reality is that the compiler optimizes the code better when using a temporary variable.

If they haven't heard the question before, it gives me a chance to assess how they work through the problem and helps me find an appropriate role for them if the interview is somewhat generic. If they dislike that problem or find it difficult to solve it, they are most likely not going to enjoy working on hardware/firmware. Most people who solve it really enjoy the problem and in my experience have performed quite well on the job.

Recently, I observed that this question is part of the prep material online and most people have seen it, so there's not much value in asking it. I still do it for fun sometimes.

Anyway, I agree with your comment though.