Hash Tables
The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h(k) = k mod 10 and forward linear probing. What is the resultant hash table?
0: 1: 2: [ 12 ] 3: [ 13 ] 4: [ 2 ] 5: [ 3 ] 6: [ 23 ] 7: [ 5 ] 8: [ 18 ] 9: [ 15 ]
Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true? 1. 9679, 1989, 4199 hash to the same value 2. 1471, 6171 hash to the same value 3. All elements hash to the same value 4. Each element hashes to a different value
1 and 2 only
What is separate chaining?
A method for handling collisions in a hash table. Each index within an array is a linked list. If two values hash to the same index, they are simply appended to the end of a linked list. It doesn't matter whether they are added to the front or end since the list needs to be checked for duplicates anyways.
What are two basic characteristics of a good hash function?
It's fast and it evenly spreads values.
What two methods can be used to deal with collisions in a hash table?
Separate Chaining (each index is a linked list) and Open Addressing (items are placed in the closest available slot preceding their given hash index if their hash index is already taken).
What is the average time complexity for insertion/removal/search in a hash table assuming a good hash function and low load factor?
Theta(1)
What is the time complexity of hashing?
Theta(1)
A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and forward linear probing. After inserting 6 values into an empty hash table, the table is as shown below: 0: 1: 2: [ 42 ] 3: [ 23 ] 4: [ 34 ] 5: [ 52 ] 6: [ 46 ] 7: [ 33 ] 8: 9: Which one of the following choices gives a possible order in which the key values could have been inserted in the table? (A) 46, 42, 34, 52, 23, 33 (B) 34, 42, 23, 52, 33, 46 (C) 46, 34, 42, 23, 52, 33 (D) 42, 46, 33, 23, 34, 52
C
How many different insertion sequences of the key values using the same hash function and linear probing will result in the hash table given? 0: 1: 2: [ 42 ] 3: [ 23 ] 4: [ 34 ] 5: [ 52 ] 6: [ 46 ] 7: [ 33 ] 8: 9: (A) 10 (B) 20 (C) 30 (D) 40
(C). 42, 23, 34, and 46 are all in the correct positions and 52 and 33 are not. When 52 is placed, then 42, 23, and 34 must have been placed already. There are 3! ways to permute the insertion order for the first 3 elements since they will always land in the same spots, so 6 ways. No matter what 33 is going to be placed last since it would be where 52 is otherwise. So 52 can be inserted next. 46 can be inserted at any time since it is also in the correct position. Since 33 is definitely inserted last, then 46 can be placed at anytime before that, so 5 choices. Thus, the total ways to insert keys is 6 * 5 = 30.
What is Open Addressing in a Hash table?
Every hash table entry contains only one key. If there is a collision, linear probing is used to find the next available position to place the new element. Each index preceding the hash index is checked until an open index is found, wrapping around to the back if necessary. When an element is removed, a marker is left behind denoting to the next insertion that a value was there previously and the preceding elements should be checked for duplicates before inserting a new value with that hash.
What is load factor?
Load factor denotes how full the hash table is, usually (# of elements) / (# of available indices). Typically, the load factor should be kept below 0.75 or 0.8 after which the entire table should be expanded and all values rehashed.
Are duplicate keys allowed within a HashMap?
No, each key must be unique.
Can hash tables be traversed in sorted order?
No, elements are not ordered within a hash table.
Can a Theta(1) time complexity ever be guaranteed in general with a hash table?
No, time complexity is dependent on the hash function.
What is the worst case time complexity for insertion/removal/search in a hash table?
O(n)
How are indices produced by a hash function kept within the range of the array in which the values are stored?
hash % capacity