What is a hash function?

Category: QuestionsWhat is a hash function?
Editor">Editor Staff asked 1 month ago

What is a hash function?
 
(a) A function has allocated memory to keys
 
(b) A function that computes the location of the key in the array
 
(c) A function that creates an array
 
(d) A function that computes the location of the values in the array
 
The origin of the question is Hash Tables in portion Hash Tables of Data Structures & Algorithms I
 
This question was addressed to me during an interview.

1 Answers
Editor">Editor Staff answered 1 month ago

Right choice is (b) A function that computes the location of the key in the array
 
Easy explanation – In a hash table, there are fewer array positions than the keys, so the position of the key in the array has to be computed, this is done using the hash function.