some days ago, during a quite pleasant job interview, I was asked for the third time an interesting (and simple) brain puzzle. having successfully solved it before (the last time about 1 year ago), I was confident I would eventually come to the solution quickly. actually that didn't happen and after struggling with the question for some time, one of the interviewers kindly put an end to my efforts (even though I felt I was almost getting it - or was I?) and offered the right (and simple) solution.
this episode made me realize how ephemeral (my) knowledge is. there I was, in a privileged position, having already answered the question, knowing the basics of how two possible methods would solve the problem, and yet that previous experience was of no use to my performance. could it even have been self detrimental in a way that I was trying to recall previous memories and that "past-event familiarity feeling" instead of actually trying to solve the puzzle? this obviously upset me and I spent the most part of the day feeling dumb for missing something that I was so clearly supposed to know. eventually I began to question why did this happen and what other examples of supposedly acquired knowledge could (momentarily?) be just "phantom memories"?
first of all, I guess the context of a job interview and the 2-hour sleep the night before didn't really help me in this situation. still, I don't think that was sufficient to justify my major fail on that problem as I was able to correctly answer several other questions during the 1.5 hours of interview. there had to be more than that to it.
the subject of human memory, how it works and what influences the creation and recall of memories is still only vaguely understood. numerous factors influence our ability to encode, store and recall memories. for instance, walking into another room can easily constitute an "event boundary" that diminishes your ability to recall memories from the previous room.
in my case, I think the problem occurred during the encoding process. my previous solutions to this problem were reached without caring too much about the method used. I just saw it as an interesting riddle, and solved it while multitasking and without paying too much attention to how my thought was being organized. I also failed to integrate the solution with previous knowledge which may have hindered my ability to migrate the solution to long-term memory (which usually occurs during sleep).
ergo, trying to avoid doing the same mistake for the third time, I am writing this article as an experiment and expecting to be able to finally organize and move this little bits of information into long term and easily recalled memory. let's see how it goes.
the problem: given 2 variables: x = a and y = b, swap their values without recurring to any other variable.
starting with the end, here is the solution:
it works. you can try it. but more interesting than that is understanding how it works.
the "exclusive or" is a logical operation which returns true if the operands are different, and false otherwise.
in order to better understand the process let us make and store just one bit. in this case would produce the following true table:
this stores the meaning of the XOR () operand, "whether is different than ", in . the second operation, gives us this second table:
as you can see, whatever the value of , will always give the value of . take a moment to think about what does this conclusion really mean.
if is different than , , and , which is actually equivalent to the result of the english expression "is different than true(1)", or simply, "is not true", i.e. the negation of . hence, because is different than () and we are working with single-bit variables, the negation of is actually .
if is equal to , , and . this time the expression is equivalent to the result of "is different than false(0)", or simply, "is true", i.e. the same logical value as . knowing that is equal to (), we can keep 's value during the swap to .
applying this same technique to a multi-bit number makes up for the first value swap. ends up attributing to .
if we look at the third instruction in our algorithm , and knowing, from the previous step, that , we can see that this is basically the same as the previous instruction, with the only difference that in this case we have instead of as the right operand of the XOR expression. therefore, we can conclude that the result will be the negation of , if is different than , or , if equals . thus, at the end of the instruction, will always store the value of , completing our value swap.
second solution - arithmetic
this was the solution that I had explored previously and failed to remember during the interview. I rushed to an empiric, exploratory, try-and-error approach trying to recall my memories, without stopping to think what actually would make sense. after the interview, this is what made sense to me and made the problem absolutely trivial: "distances"!
by thinking about and as two points on a line, and adding a third conceptual variable to the problem representing the "distance" between those points (), we created all the mental tools needed to perform the given task.
we start by storing this "distance" in one of the variables:
then, we use this variable to change the value of from to by subtracting the difference stored in from .
after this step, we still have the "distance" from to in our variable, so we just need to use it in order to obtain from (now stored in ).
and it's done. so easy that it makes me want to forget how I ridiculously failed to solve it. the most probable however is that, because of the role emotion plays in this process, I will more easily remember my bad performance on the interview question, than the solutions above.
... I hope not!