Lately I've heard about a question in a job interview:
You are given a shuffled list of 2*N+1 numbers, that contains N pairs of numbers, and one number that dos not have a pair. Find the non-paired number using only 2 integer variables.
Scroll down only when you want to know the answer...
The solution to this, is to use one integer variable as index to scan the list, and the second variable as XOR based storage, so the algorithm would be:
for i in list:
x = x XOR list[i]
The paired numbers XOR themselves to zero, and the only non-paired number remains in x.
This is since:
v XOR v = 0
and
v XOR 0 = v
Now, while this is a nice question, with a nice trick, the real question is what is the benefit of asking this question in a job interview?
What do you understand if the interviewed person did managed to find the answer?
What do you understand if that person failed to find the answer?
Nothing.
In an interview we should pursue 3 main goals:
- Get a feeling about the kind of person. Would you have a beer with that person?
- Test the knowledge of the person in a specific field or programming language.
- See how does this person cope with thinking of complex and changing problems.
No comments:
Post a Comment