DEV Community


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

theoutlander profile image
Nick Karnik Author

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.