This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Well, a quote of the question and an answer (sorta) can be found here. The answer given does not make any sense in Java, though it could work in other languages. However even in those other languages, it strikes me as a rather poor answer. The only way you can interpret the value at a particular node is if you already understand the links at the previous node. Which really defeats the purpose of a doubly linked list, doesn't it? If you can only understand the list by reading it from the beginning, you might as well just use a simple singly-linked list.
My guess is that the given answer is in fact the intended answer. Which is to say, it's not just a bad answer, but a bad question. I think someone came up with a mildly clever idea, did not realize that it was actually quite pointless, and then made a rather inane trivia question out of it. And now you have the misfortune of having this inane question inflicted on you. I would question the intelligence of whoever is asking this question of you. If that's your professor or a job interviewer, well, I guess you will need to find a more diplomatic way to put that. There are some places where a clever use of XOR is actually useful for something. However, this isn't one of them.
Well, I think it's possible to have a clever solution to a particular problem, which is also worthy of praise. But I think that only really happens if you're putting the needs of the particular problem first, and looking for the best possible solution to that problem. Whereas if you start with [what you think is] a clever solution, and then try to invent a problem that leads to that solution... that's a recipe for disaster. The original poster's problem statement smells strongly of this latter possibility.
However even in those other languages, it strikes me as a rather poor answer. The only way you can interpret the value at a particular node is if you already understand the links at the previous node. Which really defeats the purpose of a doubly linked list, doesn't it? If you can only understand the list by reading it from the beginning, you might as well just use a simple singly-linked list.
It's not quite as bad as that -- you just need to know the node that you arrived from. Meaning, if you just know a node, it is useless, you can't go anywhere.
However, if you know the node that you arrived from, then to go to the next node, just take the xor. And to turn around to the previous node, then use the node that you arrived from. In both cases, the current node becomes the new node that you arrived from.
Another interesting point is, you actually don't know the direction that you are moving in. Traversing backwards is done in the same way as traversing forward.
Interesting... but of course, totally not possible in Java.
I'm probably nitpicking, but the solution uses TWO references encoded in only one value. I still need to have the previous and next value.
(Only a C/C++ programmer could have come up with that monstrosity! )