Interview Question: The Swap

Went to an interview at Verix today. It went rather well, I think. I’m over the interview cliches and I make that known so I also try to be honest because if they don’t want me as me, they would definitely wouldn’t want me pretending to be someone else and failing in a month. So I look for a character (or as my tech interviewer said, interests) compatibility. But, as usual, there is the usual interview tech question. This one, one I’ve seen before, is actually rather earnest and seemed to me to test actual knowledge. This one, tests knowledge of variable passing. The idea is that we want a function that receives two variable references (strings in my case) and we need to swap them around. The problem that is needed to be understood here is that swapping the references, which will happen if you go with your first thought, doesn’t help. temp = var1; var1 = var2; var2 = temp; // doesn’t do anything once you exit the function Because what ever you do, if you do it that way, you will switch around references in your hand and do nothing to the original pointers and data. What needs to be done is to access the underlying data and modify that. for (int i; i < var1.length; i++) { tempPart = var1[0]; var1[0] = var2[0]; var2[0] = tempPart; } // this should get things done but be careful of access violations which are not protected against here. That Continue Reading →


Posted in No Category, Programming, Thinking Out Loud by with 2 comments.