Hash Maps Flashcards
What is a map data type? Whats another name for it? What does a map data type consist of?
Map data types are data types found in programming languages. In python, maps are known as dictionaries. Maps can also be known as associative array. Maps consist of key-value pairs.
Is hash map the same as hash table?
Yes it’s the same thing
What is time complexity of lookup/search in a hash table?
O(1) on average because to get the index of a key, we run it through the hash function which is a fixed number of steps, and once we know the index, searching at a known index is constant
Whats the work flow for getting an index in a hash map based on a given key (two step process)
Pass the key through the hash function
Take modulo of the hashed key % capacity of hash map….this gives you the index relative to the array size
hash = hash_function(key) index = hash % array_size
What is a hash function?
A hash function takes a key value of some type and returns an integer that corresponds to an index in an array
When choosing/designing a hash function, what are the 3 properties you should have for an optimal function?
1) Determinism- The hash function should be consistent in the hashing that it does for values. A given input should always map to the same hash value.
2) Uniformity- the inputs should be mapped as evenly as possible over the output range. A non-uniform function can result in many collisions, where multiple elements are hashed to the same array index. So basically the hash function should as best as possible uniformly DISTRIBUTE filling buckets across the whole array
3) Speed- The hash function should have a low computational burden…in other words it should be fast
What is a perfect hash function?
A perfect hash function is one that results in no collisions…every key gets a unique output.
What is a minimally perfect hash function?
A minimally perfect hash function is one that results in no collisions for a table size that equals exactly the number of elements.
A minimally perfect hash function is one that results in no collisions for a table size that exactly equals the number of elements. A minimal perfect hash function is a perfect hash function that maps n keys to n consecutive integers – usually the numbers from 0 to n − 1 or from 1 to n.
What is a collision?
A collision is when two different keys are hashed to the same index. i.e same output for two different inputs.
What is chaining and what is it used to address?
Chaining is a way to resolve collisions in a hash table. Chaining allows keys to be hashed to the same index by creating linked lists or buckets at every index. When two different keys are hashed to the same index, the key/values are simply chained as nodes in the bucket where they belong.
To accommodate multiple keys, linked lists can be used to store the individual keys that map to the same entry. The linked lists are commonly referred to as buckets or chains, and this technique of collision resolution is known as chaining.
Are linked lists the only data structure you can use to address chaining?
No, you can also use balanced binary trees or dynamic arrays
In a chained hash table, accessing the value for a particular key would follow this procedure:
1) hash the key to get the index of the bucket
2) search the linked list in the correct bucket for the key. There may be multiple keys hashed into the same bucket, so we need to search the linked list for the specific key that was passed in
What is the “load factor” of a hash table. Whats its equation?
Load factor is the average number of elements in each bucket
The equation is 𝝺 = total number of elements in table/number of buckets
In a chained hash table, can the load factor be greater than 1?
Yes in a chained hash table, the load factor can be greater than 1.
As load factor increases, what happens to the speed of operations on the table?
Speed decreases.