62

This might sound as an very vague question upfront but it is not. I have gone through Hash Function description on wiki but it is not very helpful to understand.

I am looking simple answers for rather complex topics like Hashing. Here are my questions:

  1. What do we mean by hashing? How does it work internally?
  2. What algorithm does it follow ?
  3. What is the difference between HashMap, HashTable and HashList ?
  4. What do we mean by 'Constant Time Complexity' and why does different implementation of the hash gives constant time operation ?
  5. Lastly, why in most interview questions Hash and LinkedList are asked, is there any specific logic for it from testing interviewee's knowledge?

I know my question list is big but I would really appreciate if I can get some clear answers to these questions as I really want to understand the topic.

Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
Rachel
  • 100,387
  • 116
  • 269
  • 365
  • 3
    Try [Hash table](http://en.wikipedia.org/wiki/Hash_table) on Wikipedia instead. A hash function is used as part of the process, but does not explain "how" a hash table works. –  Dec 15 '10 at 19:05
  • 1
    There is no such thing as a `HashList` in Java or any other language I am aware of. Don't use code formatting for text that isn't code. – user207421 Jun 12 '17 at 05:27

5 Answers5

36
  1. Here is a good explanation about hashing. For example you want to store the string "Rachel" you apply a hash function to that string to get a memory location. myHashFunction(key: "Rachel" value: "Rachel") --> 10. The function may return 10 for the input "Rachel" so assuming you have an array of size 100 you store "Rachel" at index 10. If you want to retrieve that element you just call GetmyHashFunction("Rachel") and it will return 10. Note that for this example the key is "Rachel" and the value is "Rachel" but you could use another value for that key for example birth date or an object. Your hash function may return the same memory location for two different inputs, in this case you will have a collision you if you are implementing your own hash table you have to take care of this maybe using a linked list or other techniques.

  2. Here are some common hash functions used. A good hash function satisfies that: each key is equally likely to hash to any of the n memory slots independently of where any other key has hashed to. One of the methods is called the division method. We map a key k into one of n slots by taking the remainder of k divided by n. h(k) = k mod n. For example if your array size is n = 100 and your key is an integer k = 15 then h(k) = 10.

  3. Hashtable is synchronised and Hashmap is not. Hashmap allows null values as key but Hashtable does not.

  4. The purpose of a hash table is to have O(c) constant time complexity in adding and getting the elements. In a linked list of size N if you want to get the last element you have to traverse all the list until you get it so the complexity is O(N). With a hash table if you want to retrieve an element you just pass the key and the hash function will return you the desired element. If the hash function is well implemented it will be in constant time O(c) This means you dont have to traverse all the elements stored in the hash table. You will get the element "instantly".

  5. Of couse a programer/developer computer scientist needs to know about data structures and complexity =)

Enrique
  • 9,920
  • 7
  • 47
  • 59
  • Both the links you have provided take me to wiki page which i have already visited and have mentioned in question that i have gone through them so can you update your first 2 points ? – Rachel Dec 15 '10 at 18:43
  • Thanks for updating the answer, now am able to get more out of it. – Rachel Dec 15 '10 at 19:19
  • 1
    There is no such thing as 'O(c) time complexity': you mean O(1). – user207421 Jun 12 '17 at 05:26
13
  1. Hashing means generating a (hopefully) unique number that represents a value.
  2. Different types of values (Integer, String, etc) use different algorithms to compute a hashcode.
  3. HashMap and HashTable are maps; they are a collection of unqiue keys, each of which is associated with a value.
    Java doesn't have a HashList class. A HashSet is a set of unique values.
  4. Getting an item from a hashtable is constant-time with regard to the size of the table.
    Computing a hash is not necessarily constant-time with regard to the value being hashed.
    For example, computing the hash of a string involves iterating the string, and isn't constant-time with regard to the size of the string.
  5. These are things that people ought to know.
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    No, it won't. It is not possible to generate a unique _32-bit_ number for every possible string. That's why collisions exist. – SLaks Dec 15 '10 at 18:35
  • Ok. What would be an algorithm to compute `hashcode` of `long` – Rachel Dec 15 '10 at 18:37
  • 1
    @Rachel: Hash don't try to generate unique number. It tries to create a homogenous distribution for ouput, so that each output value would have roughly `1/nr_of_possible_hash_values` probability. – ruslik Dec 15 '10 at 18:57
  • Often you can use the number direct as a hashcode. long has a limited range, so this would be a perfect hashfunction with a 1:1 mapping and no collisions. – Juri Robl Dec 15 '10 at 18:58
  • @ruslik: Can you explain in simpler terms ? – Rachel Dec 15 '10 at 19:00
  • @Juri: You can't use the number directly as a hashcode if it's a `long` (at least not in Java) because a hashcode has to be an `int`, so you obviously have to reduce the precision while maintaining reasonably good collision behavior. – ColinD Dec 15 '10 at 19:05
  • @ColinD: Oh, you're right. My comment wasn't targeted at Java, but at hashs generally. I have to admit that I've never used hashcodes in Java. – Juri Robl Dec 15 '10 at 19:08
  • 4
    @Rachel - what @ruslik means is this: a hash outputs a number, and that number is within a specific range of numbers. As an example, that number might be between 0 and 2^32-1. When you hash some data, a number in that range is returned, and ideally every single possible number is equally likely to be returned. The reason you want this for Hash Tables/Maps is that the table can be thought of as an array. When you hash a value, you use that number as the index in the array. You want to avoid using the same index twice. If the numbers a uniformly distributed then a collision is less likely. – Niki Yoshiuchi Dec 15 '10 at 19:43
  • Hashing means generating a number in a relatively small range from an input in a much larger range, with the hope that the hashes are well-distributed. Uniqueness is not an explicit goal. – user207421 Jun 12 '17 at 05:26
  • @JuriRobl: "a perfect hashfunction with a 1:1 mapping and no collisions" - what matters in practice is the distrubution & probability of collisions _after_ the hash value is mapped to the number of buckets in use; using a numerical key directly as the hash code is common (because there's no overhead for hash calculation and if the values are often contiguous they may map nicely onto consecutive buckets, actually reducing collisions below the rates using cryptographic strength hashing) but risky (you could get a _*lot*_ of collisions and completely undermine your hash table's O(1) properties). – Tony Delroy May 02 '20 at 11:34
6
  1. Hashing is transforming a given entity (in java terms - an object) to some number (or sequence). The hash function is not reversable - i.e. you can't obtain the original object from the hash. Internally it is implemented (for java.lang.Object by getting some memory address by the JVM.

  2. The JVM address thing is unimportant detail. Each class can override the hashCode() method with its own algorithm. Modren Java IDEs allow for generating good hashCode methods.

  3. Hashtable and hashmap are the same thing. They key-value pairs, where keys are hashed. Hash lists and hashsets don't store values - only keys.

  4. Constant-time means that no matter how many entries there are in the hashtable (or any other collection), the number of operations needed to find a given object by its key is constant. That is - 1, or close to 1

  5. This is basic computer-science material, and it is supposed that everyone is familiar with it. I think google have specified that the hashtable is the most important data-structure in computer science.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Can you example algorithm implementation for generating hashcode function from long ? Also I was asked in interview what is the algorithm for generating hashcode, I was not sure of currently internal working and so wanted to understand how is it done internally. – Rachel Dec 15 '10 at 18:39
  • 2
    you can look at `java.lang.Long`'s documentation for that - the code is one-line: `return (int)(value ^ (value >>> 32));` – Bozho Dec 15 '10 at 18:40
6

I'll try to give simple explanations of hashing and of its purpose.

First, consider a simple list. Each operation (insert, find, delete) on such list would have O(n) complexity, meaning that you have to parse the whole list (or half of it, on average) to perform such an operation.

Hashing is a very simple and effective way of speeding it up: consider that we split the whole list in a set of small lists. Items in one such small list would have something in common, and this something can be deduced from the key. For example, by having a list of names, we could use first letter as the quality that will choose in which small list to look. In this way, by partitioning the data by the first letter of the key, we obtained a simple hash, that would be able to split the whole list in ~30 smaller lists, so that each operation would take O(n)/30 time.

However, we could note that the results are not that perfect. First, there are only 30 of them, and we can't change it. Second, some letters are used more often than others, so that the set with Y or Z will be much smaller that the set with A. For better results, it's better to find a way to partition the items in sets of roughly same size. How could we solve that? This is where you use hash functions. It's such a function that is able to create an arbitrary number of partitions with roughly the same number of items in each. In our example with names, we could use something like

int hash(const char* str){
    int rez = 0;
    for (int i = 0; i < strlen(str); i++)
        rez = rez * 37 + str[i];
    return rez % NUMBER_OF_PARTITIONS;
};

This would assure a quite even distribution and configurable number of sets (also called buckets).

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
ruslik
  • 14,714
  • 1
  • 39
  • 40
0

What do we mean by Hashing, how does it work internally ?

Hashing is the transformation of a string shorter fixed-length value or key that represents the original string. It is not indexing. The heart of hashing is the hash table. It contains array of items. Hash tables contain an index from the data item's key and use this index to place the data into the array.

What algorithm does it follow ?

In simple words most of the Hash algorithms work on the logic "index = f(key, arrayLength)"

Lastly, why in most interview questions Hash and LinkedList are asked, is there any specific logic for it from testing interviewee's knowledge ?

Its about how good you are at logical reasoning. It is most important data-structure that every programmers know it.