Data Structures Exam 2 Review - Hash table
Direct Hashing Type the hash table after the given operations. numsTable: 0: 0 1: 2: 2 3: 3 4: HashRemove(numsTable, 0) HashInsert(numsTable, item 4)
0: 1: 2: 2 3: 3 4: 4
Linear Probing Given hash function of key % 5, determine the insert location for each item. HashInsert(numsTable, item 13) numsTable: 0: 1: 71 2: 22 3: 4: bucket =
3
Given a hash table with 100 buckets and modulo hash function, in which bucket will HashInsert(table, item 334) insert item 334?
34
Chaining Hash table valsTable is shown below. The hash function is key % 5. valsTable: 0: -> 35 -> 40 1: 2: -> 12 -> 27 -> 42 3: -> 8 -> 18 -> 38 -> 43 4: -> 24 Maximum number of comparisons required to search for a key: Minimum number of comparisons required to search for a key:
4 0 -- Bucket 1 is empty, so no comparisons are made when a key maps to bucket 1. Thus, the minimum number of comparisons required to search for a key is 0.
Chaining Hash table valsTable is shown below. The hash function is key % 5. Assume items are inserted at the end of a bucket's list. valsTable: 0: -> 5 -> 25 -> 45 -> 60 1: -> 11 -> 31 -> 36 2: -> 17 3: 4: -> 14 -> 29 After the operations HashRemove(valsTable, item 25) HashRemove(valsTable, item 20) What is bucket 0's list?
5, 45, 60
A modulo hash function for a 50 entry hash table is: key % _____
50
Encryption and decryption are synonymous.
False
A hash value can be used to reconstruct the original data
False Although true for some hashing functions, MD5 and many others produce a hash value that cannot be used to reconstruct the original data.
A company will store all employees in a hash table. Each employee item consists of a name, department, and employee ID number. Which is the most appropriate key?
ID
A hash table has buckets 0 to 9 and uses a hash function of key % 10. If the table is initially empty and the following inserts are applied in the order shown, the insert of which item results in a collision? HashInsert(hashTable, item 55) HashInsert(hashTable, item 90) HashInsert(hashTable, item 95)
Item 95
Will the hash function and expected key likely work well for the following scenarios? Hash function: key % 1000 Key: Customer's 3-digit U.S. phone number area code, of which about 300 exist. Hash table size: 1000
No Every key is ideally unique, but numerous customers may have the same area code. Also, less than a third of 1000 buckets will be used; the U.S. has only about 300 area codes.
Will the hash function and expected key likely work well for the following scenarios? Hash function: key % 1000 Key: Selling price of a house. Hash table size: 1000
No Most house prices are rounded to the nearest 1000, as in $249,000. Thus, nearly all prices will map to bucket 0.
In a hash table using open addressing, the load factor cannot exceed 1.0.
True With open addressing, one bucket holds at most one item. So a table with N buckets can have at most N items, making the maximum possible load factor N / N = 1.0.
Will the hash function and expected key likely work well for the following scenarios? Hash function: key % 250 Key: 5-digit customer ID Hash table size: 250
Yes
A 100 element hash table has 100 _____.
buckets
A hash function computes a bucket index from an item's _____.
key
If a message is encrypted with a left shift of X, what shift is needed to decrypt?
right shift of X
Chaining Hash table valsTable is shown below. The hash function is key % 5. Assume items are inserted at the end of a bucket's list. valsTable: 0: -> 30 1: 2: -> 47 3: 4: -> 9 After the operations HashInsert(valsTable, item 75) HashInsert(valsTable, item 11) Select the bucket containing the following items 30 75 11
0 0 1
Given a hash table with 50 buckets and modulo hash function, in which bucket will HashSearch(table, 201) search for the item?
1
Linear Probing Given hash function of key % 5, determine the insert location for each item. HashInsert(numsTable, item 74) numsTable: 0: 20 1: 2: 32 3: 4: 94 bucket =
1
Chaining Hash table valsTable is shown below. The hash function is key % 5. valsTable: 0: -> 10 -> 15 -> 40 -> 45 1: -> 11 -> 26 -> 36 2: -> 12 -> 32 3: 4: -> 9 -> 14 -> 39 How many comparisons does ListSearch perform to determine If item 10 is in valsTable? If item 35 is in valsTable? If item 14 is in valsTable?
1 4 2
Quadratic Probing Consider the following hash table, a hash function of key % 10, and quadratic probing with c1 = 1 and c2 = 1. valsTable: 0: 60 1: empty-after-removal 2: 110 3: 4: 364 5: 75 6: 66 7: empty-after-removal 8: 9: 49 1) HashSearch(valsTable, 75) probes _____ buckets. 2) HashSearch(valsTable, 110) probes _____ buckets. 3) After removing 66 via HashRemove(valsTable, 66), HashSearch(valsTable, 66) probes _____ buckets.
1) 1 2) 2 3) 2 - stops at bucket 8 because it was empty-since-start
Linear Probing Consider the following hash table and a hash function of key % 10. valsTable: 0: 60 1: empty-after-removal 2: 110 3: 4: 364 5: 75 6: 66 7: empty-after-removal 8: 9: 49 1) HashSearch(valsTable, 75) probes _____ buckets. 2) HashSearch(valsTable, 110) probes _____ buckets. 4) HashSearch(valsTable, 112) returns? 3) HashSearch(valsTable, 207) probes _____ buckets.
1) 1 2) 3 3) null 4) 2
Chaining Consider the following hash table, and a hash function of key % 10. valsTable: 0: -> 60 -> 300 -> 40 1: 2: -> 62 3: -> 13 -> 713 4: 5: 6: -> 66 7: -> 147 -> 67 8: 9: -> 409 1) How many list elements are compared for HashSearch(valsTable, 62)? 2) How many list elements are compared for HashSearch(valsTable, 40)? 3) What does HashSearch(valsTable, 186) return? 4) How many list elements are compared for HashSearch(valsTable, 837)?
1) 1 2) 3 3) null 4) 2
Consider a modulo hash function (key % 10) and the following hash table. 0: 1: 11 2: 22 3: 4: 5: 45 6: 7: 47 8: 9: 39 1) How many buckets will be checked for HashSearch(numsTable, 45)? 2) If item keys range from 0 to 49, how many keys may map to the same bucket? 3) If a linear search were applied to the array, how many array elements would be checked to find item 45?
1) 1 2) 5 3) 6
Suppose the hash table below is resized. The hash function used both before and after resizing is: hash(key) = key % N, where N is the table size. 0: 1: 99 2: 51 3: 4: 5: 75 6: 27 1) What is the most likely allocated size for the resized hash table? 2) How many elements are in the hash table after resizing? 3) At what index does 99 reside in the resized table?
1) 17 -- tablesize * 2 = 14. Resizing will use the next prime number >= 14, which is 17. 2) 4 3) 14 -- 99 % 17 = 14
Double Hashing Consider the following hash table, a first hash function of key % 10, and a second hash function of 7 - key % 7. valsTable: 0: 60 1: empty-after-removal 2: 3: 223 4: 104 5: 6: 66 7: empty-after-removal 8: empty-after-removal 9: 49 1) HashSearch(valsTable, 110) probes _____ buckets. 2) HashInsert(valsTable, item 24) probes _____ buckets. 3) After removing 66 via HashRemove(valsTable, 66), HashSearch(valsTable, 66) probes _____ buckets.
1) 2 2) 2 3) 5
For a 1000-entry hash table, compute the multiplicative hash for the following strings using the specific initial value and hash multiplier. 1) Initial value = 0. Hash multiplier = 1. String = BAT
1) 215 -- A hash multiplier of 1 will simply add the characters' ASCII values. InitialValue + B + A + T = 0 + 66 + 65 + 84 = 215. 215 % 1000 = 215 2) 215 3) 483 -- stringHash = InitialValue = 17. stringHash = 17 * 3 + W = 17 * 3 + 87 = 138. stringHash = 138 * 3 + E = 138 * 3 + 69 = 483. 483 % 1000 = 483.
Linear Probing Consider the following hash table and a hash function of key % 10. idsTable: 0: 20 1: 68 2: 22 3: 4: 34 5: empty-after-removal 6: 115 7: 65 8: 48 9: 199 1) HashRemove(idsTable, 65) probes _____ buckets. 2) HashRemove(idsTable, 10) probes _____ buckets. 3) HashRemove(idsTable, 68) probes _____ buckets.
1) 3 2) 4 - stop at empty-since-start 3) 4
Chaining Given hash function of key % 10, type the specified bucket's list after the indicated operation(s). Assume items are inserted at the end of a bucket's list. valsTable: 0: -> 40 1: 2: 3: -> 53 -> 363 4: 5: 6: -> 46 -> 16 7: 8: -> 218 9: 1) HashInsert(valsTable, item 20) Bucket 0's list: _____ 2) HashInsert(valsTable, item 23) HashInsert(valsTable, item 99) Bucket 3's list: _____ 3) HashRemove(valsTable, 46) Bucket 6's list: _____ 4) HashRemove(valsTable, 218) Bucket 8's list: _____
1) 40, 20 2) 53, 363, 23 3) 16 4) EMPTY
Double Hashing Given: hash1(key) = key % 11 hash2(key) = 5 - key % 5 and a hash table with a size of 11. Determine the index for each item after the following operations have been executed. HashInsert(valsTable, item 16) HashInsert(valsTable, item 77) HashInsert(valsTable, item 55) HashInsert(valsTable, item 41)HashInsert(valsTable, item 63) 1) Item 16 2) Item 55 3) Item 63
1) 5 2) 10 3) 1
Quadratic Probing Assume a hash function returns key % 16 and quadratic probing is used with c1 = 1 and c2 = 1. Refer to the table below. 0: 32 1: 49 2: 16 3: 3 4: 5: 99 6: 64 7: 23 8: 9: 10: 42 11: 11 12: 13: 14: 15: 1) 32 was inserted before 16 2) Which value was inserted without collision? 3) What is the probing sequence when inserting 48 into the table? 4) How many bucket index computations were necessary to insert 64 into the table? 5) If 21 is inserted into the hash table, what would be the insertion index?
1) True 2) 23 3) 0, 2, 6, 12 4) 3 5) 9
Linear Probing Given hash function of key % 10, determine the bucket status after the following operations have been executed. HashInsert(valsTable, item 64) HashInsert(valsTable, item 20) HashInsert(valsTable, item 51) HashRemove(valsTable, 51) 1) Bucket 2 2) Bucket 1 3) Bucket 4
1) empty-since-start 2) empty-after-removal 3) occupied
A modulo hash function is used to map to indices 0 to 9. The hash function should be: key % _____
10
For a decimal mid-square hash function, what is the bucket index for key = 110, N = 200, and R = 3?
10 110 * 110 = 12100 Middle 3 digits = 210 bucket index is 210 % 200 = 10
For R = 3, what are the middle bits for a key of 9? 9 * 9 = 81; 81 in binary is 1010001.
100
Direct Hashing A class has 100 students. Student ID numbers range from 10000 to 99999. Using the ID number as key, how many buckets will a direct access table require?
100000
A hash table's items will be positive integers, and -1 will represent empty. A 5-bucket hash table is: -1, -1, 72, 93, -1. How many items are in the table?
2
Linear Probing Given hash function of key % 5, determine the insert location for each item. HashInsert(numsTable, item 90) numsTable: 0: 50 1: 31 2: 3: 4: 4 bucket =
2
Linear Probing Given hash function of key % 5, determine the insert location for each item. HashInsert(numsTable, item41) numsTable: 0: 1: 21 2: 3: 4: bucket =
2
Direct Hashing How many direct access table buckets are needed for items with keys ranging from 100 to 200 (inclusive)?
201
Chaining Hash table valsTable is shown below. The hash function is key % 5. Assume items are inserted at the end of a bucket's list. valsTable: 0: -> 20 1: -> 21 -> 31 -> 36 -> 46 2: -> 7 -> 22 -> 37 3: 4: -> 24 -> 49 After the operations HashInsert(valsTable, item 41) What is bucket 1's list?
21, 31, 36, 46, 41
If the Caesar cipher were implemented such that strings were restricted to only lower-case alphabet characters, how many distinct ways could a message be encrypted?
25 26 lower-case alphabet characters exist. So each can be shifted to 25 other characters before repeating.
For a decimal mid-square hash function, what is the bucket index for key = 112, N = 1000, and R = 3?
254
For a decimal mid-square hash function, what are the middle digits for key = 40, N = 100, and R = 2?
60 40 * 40 = 1600 Middle 2 (=R) digits = 60 bucket index is 60 % 100 = 60
For a binary mid-square hash function, how many bits are needed for an 80 entry hash table?
7 R = log280 = 6.32 = 7
Direct Hashing For a 1000-entry direct access table, type the bucket number for the inserted item, or type: None HashInsert(hashIndex, item 734)
734
key % 1000 maps to indices 0 to ____.
999
Which is not an advantage of storing password hash values, instead of actual passwords, in a database?
Database storage space is saved
Double Hashing When the removal algorithm finds the bucket containing the key to be removed, the bucket is marked as empty-since-start.
False
In a hash table using chaining, the load factor cannot exceed 1.0.
False
Quadratic Probing The insertion algorithm can only insert into empty-since-start buckets.
False
Quadratic Probing The search algorithm stops only when encountering a bucket containing the key being searched for.
False
If computer B in the above example computed a hash value identical to the downloaded hash value, then the downloaded message would be guaranteed to be uncorrupted.
False Two identical MD5 hash values imply a high likelihood that the data is uncorrupted, but not a guarantee. Different hash values, on the other hand, guarantee that the data is corrupted.
MD5 produces larger hash values for larger input data sizes.
False MD5 always produces a 128-bit hash value.
Suppose a hash table has 101 buckets. If the hash table was using open addressing, a load factor > 0.9 guarantees a collision during insertion.
False Unless the load factor is 1.0, empty buckets exist in the table.
A hash table implementation must use only one criteria for resizing.
False - exceed Load Factor (items/buckets) - exceeding number of collisions - exceeding number of items in list when chaining
Quadratic Probing When a hash table is initialized, all entries must be empty-after-removal.
False - empty-since-start
The Caesar cipher is an encryption algorithm that works well to secure data for modern digital communications.
False -- easy to decrypt
An empty hash table hashTable has 15 buckets and a hash function of key % 15. The following operations are performed in order. Select which operations cause a collision. HashInsert(hashTable, 28) HashInsert(hashTable, 13) HashInsert(hashTable, 43) HashInsert(hashTable, 5) HashInsert(hashTable, 22)
HashInsert(hashTable, 13) HashInsert(hashTable, 43)
An ordinary array contains items with keys: 78, 9, 3, 10, 8, 20, 2, 37, 66 A hash table has 20 buckets with hash function key % 20. The hash table contains the same items as the array. How many elements are checked when searching for each of the following items? Item Array. Hash 4. ? ? 66 ? ? 8 ? ?
Item Array. Hash 4. 9 1 66 9 1 8 5 1 In the hash table, only one element is checked regardless of the item searched for. If an item maps to a bucket that contains that item, the item is in the table. If an item maps to a bucket that contains a different item, the item is not in the table. If an item maps to an empty bucket, the item is not in the table.
Will the hash function and expected key likely work well for the following scenarios? Hash function: key % 1000 Key: 6-digit employee ID Hash table size: 20000
No Numerous collisions are likely. The hash function only maps keys into the first 1000 buckets. A good hash function should uniformly distribute hashed keys across all buckets.
Will the hash function and expected key likely work well for the following scenarios? Hash function: key % 40 Key: 4-digit even numbers Hash table size: 40
No The remainder from dividing two even numbers is an even number. So, keys only map to even buckets.
Direct Hashing For a 1000-entry direct access table, type the bucket number for the inserted item, or type: None HashInsert(hashIndex, item 1034)
None
Direct Hashing For a 1000-entry direct access table, type the bucket number for the inserted item, or type: None HashInsert(hashIndex, item -45)
None
For a well-designed hash table, searching requires _____ on average.
O(N)
A user could login with an incorrect password if a password hashing function produced the same hash value for two different passwords.
True
Cryptography is used heavily in internet communications.
True
Double Hashing Double hashing would never resolve collisions if the second hash function always returned 0.
True
Generating and storing random data alongside each password hash in a database, and using (password + random_data) to generate the hash value, can help increase security.
True
Quadratic Probing The removal algorithm searches for the bucket containing the key to remove. If found, the bucket is marked as empty-after-removal.
True
Suppose a hash table has 101 buckets. If the hash table was using chaining, the load factor could be ≤ 0.1, but an individual bucket could still contain 10 items.
True
When resizing to a larger size, the load factor is guaranteed to decrease.
True
Suppose a hash table has 101 buckets. If the hash table was using open addressing, a load factor < 0.25 guarantees that no more than 25 collisions will occur during insertion.
True A load factor < 0.25 means fewer than 25% of the 101 buckets are in use. Therefore, at most 25 buckets are in use
What is the result of applying the Caesar cipher with a left shift of 1 to the string "computer"?
bnlotsdq -- minus 1 fom eahc letter ex. c - 1 = b
Direct Hashing Type the hash table after the given operations. numsTable: 0: 1: 1 2: 2 3: 4: HashInsert(numsTable, item 0)
numsTable: 0: 0 1: 1 2: 2 3: 4:
Linear Probing For the given hash table and hash function of key % 5, what are the contents for each bucket after the following operations? numsTable: 0: 20 1: empty-after-removal 2: 3: empty-after-removal 4: HashInsert(numsTable, item 43) HashInsert(numsTable, item 300) HashInsert(numsTable, item 71)
numsTable: 0: 20 1: 300 2: 71 3: 43 4: empty-since-start