Hash Maps Flashcards
What is the ‘Set’ ADT and its operations?
A collection of data items that does not allow duplicates.
Operations: insert(x), remove(x), contains(x), isEmpty(), size(), addAll(x)/union(x), retainAll(x)/intersection(x), removeAll()
What is the ‘Ordered Set’ ADT, its additional operations, and its important operation runtimes?
A set with a ‘total order’ defined on the items. (All items pairs in a <, >, relation)
Operations: findMin(), findMax(x)
Runtime for balanced search trees Oln(n): insert(x), remove(x), contains().
What is the ‘Map ADT’ and describe its operations?
The map ADT is a collection of (key, value) pairs.
Keys are unique (sets), but values are not.
Operations: get(key), set(key, value)
What is a common way to store a map whose keys are integers?
In an array, so that the runtime for get(k), and put(k, v) are Theta(1).
What is a ‘Hash Table’ and what is its lookup runtime?
A data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values.
Look/ Get: Theta(1);
What are hashing ‘collisions’?
When more than one value is hashed by a particular hash function hashes to the same slot in the table.
What are the two goals of a good hash function?
- Uniform Distribution i.e. to spread the keys out as much as possible
- Ensure that all table cells can be used.
What are the four practices to be cognizant of when designing a hash table?
- Keep tableSize a prime number
- When combining hash values, make the factors prime numbers.
- Only use immutable objects as keys
- When two objects are equal, their hash code methods must return the same values.
Define the two methods to resolve collisions in hash tables.
- Separate Chaining: Keeps all items whose key hashes to the same value in a linkedList.
- Linear Probing: Placing the new key in another empty cell bases on a secondary function.
What is the equation to calculate the ‘Load Factor’?
Load Factor (lambda) = The average length of a list = N/tableSize.