Interview : Hash function: sine function
c, trigonometry
Solution
If you mean `sin()`, it's not a good hashing function because:
- it's quite predictable and for some `x` it's no better than just `x` itself. There should be no seemingly apparent relationship between the key and the hash of the key.
- it does not produce an integer value. You cannot index/subscript arrays with floating-point indices and there must be some kind of array in the hash table.
- floating-point is very implementation-specific and even if you make a hash function out of `sin()`, it may not work with a different compiler or on a different kind of CPU/computer.
- `sin()` may be much slower than some simpler integer-arithmetic function.
Problem
I was asked this interview question. I am not sure what the correct answer for it is (and the reasoning behind the answer): Is sin(x) a good hash function?