Ruby Developer Question:
Download Job Interview Questions and Answers PDF
Tell us how would you implement hash in Ruby internally?
Answer:
The purpose of a hash function is to convert a given a key into an integer of limited range. In order to reduce the range, we use a technique called the division method. In the division method, the key is divided by the size of the storage and the remainder is the location inside that table where a record can be stored.
But in real life programming the keys includes strings, objects, etc as well. This is solved by using a one-way hash function over the key and then applying the division method to get the location. The hash function is a mathematical function that takes a string of any length and produces a fixed length integer value. The hash data structure derives it's name from this hashing mechanism. Ruby uses the murmur hash function and then applies the division method with a prime number M, which Ruby determines based on the table size that is needed for storage.
But in real life programming the keys includes strings, objects, etc as well. This is solved by using a one-way hash function over the key and then applying the division method to get the location. The hash function is a mathematical function that takes a string of any length and produces a fixed length integer value. The hash data structure derives it's name from this hashing mechanism. Ruby uses the murmur hash function and then applies the division method with a prime number M, which Ruby determines based on the table size that is needed for storage.
Download Ruby Developer Interview Questions And Answers
PDF
Previous Question | Next Question |
Tell me how do you remove nil values in array using ruby? | What is the difference between Procs and Blocks? |