Hashing and Hash Tables
- 73Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is hashing?
What is hashing?
73 questions
- 1
What is hashing?
- A
Storing every key in a linked list
- B
Converting a key into a fixed-size value
- C
Sorting all values into ascending order
- D
Comparing each key with every other key
Correct answer
BConverting a key into a fixed-size value
Explanation
Hashing is the process of converting a key into a fixed-size value called a hash.
- A
- 2
What is the result produced by a hashing process called?
- A
A hash
- B
A node
- C
A capacity
- D
A pointer
Correct answer
AA hash
Explanation
The fixed-size value produced by hashing a key is called a hash.
- A
- 3
What does a hash function do?
- A
It takes a key, performs computations, and returns its hash
- B
It traverses every element before returning an index
- C
It sorts all keys according to their lengths
- D
It takes a value and stores it directly in the first slot
Correct answer
AIt takes a key, performs computations, and returns its hash
Explanation
A hash function accepts a key, performs computations, and returns the hash of that key.
- A
- 4
Why must a hash function be deterministic?
- A
The function must use the same amount of memory as the table
- B
The hash table must always contain the same number of slots
- C
The same key must produce the same hash during insertion and lookup
- D
Every different key must produce a different hash
Correct answer
CThe same key must produce the same hash during insertion and lookup
Explanation
If the same key produced different hashes, insertion and later lookup could use different positions, making the value impossible to find reliably.
- A
- 5
For a hash table with m slots, what range of indices should a hash function produce?
- A
From −m through m
- B
From 0 through m
- C
From 0 through m−1
- D
From 1 through m
Correct answer
CFrom 0 through m−1
Explanation
A table with m slots has valid indices from 0 to m−1.
- A
- 6
In the example hash function, how is the hash of a string calculated?
- A
Count the characters and add the table capacity
- B
Sum the ASCII codes and take the result modulo m
- C
Multiply the ASCII codes and divide by the string length
- D
Sort the characters and use the first character's code
Correct answer
BSum the ASCII codes and take the result modulo m
Explanation
The example function adds the ASCII code of each character and returns the sum modulo m.
- A
- 7
Using the example function, what hash is obtained for “inside code” when m=30 and the ASCII sum is 1079?
- A
19
- B
29
- C
30
- D
1079
Correct answer
B29
Explanation
Since 1079 mod 30 = 29, the key maps to index 29.
- A
- 8
What is the main advantage of using hashing to locate an item?
- A
It eliminates the need to store the item associated with a key
- B
It guarantees that every key receives a unique position
- C
It ensures that the table requires no additional memory
- D
It can calculate a likely position without scanning all earlier items
Correct answer
DIt can calculate a likely position without scanning all earlier items
Explanation
Hashing directly calculates an index from the key, avoiding a linear scan through preceding items.
- A
- 9
What does a hash table store?
- A
Only numerical indices
- B
Only sorted keys
- C
A sequence of unassociated values
- D
Key/value pairs
Correct answer
DKey/value pairs
Explanation
A hash table is a data structure that stores key/value pairs.
- A
- 10
How is a value located in a hash table?
- A
The table scans every value until the key is found
- B
The key is converted into a linked-list pointer
- C
The key is hashed to calculate the value's array index
- D
The value is always stored at the key's character count
Correct answer
CThe key is hashed to calculate the value's array index
Explanation
The hash function maps the key to an index in the array of slots where the associated value should be located.
- A
- 11
In the library analogy, what corresponds to the value in a key/value pair?
- A
The book itself
- B
The library position
- C
The hash function
- D
The book's title
Correct answer
AThe book itself
Explanation
The book title is the key, the book itself is the value, and the library represents the array of slots.
- A
- 12
Which mapping correctly represents a phone agenda implemented with a hash table?
- A
Array index as key and person's name as value
- B
Phone number as key and person's name as value only during lookup
- C
Person's name as key and phone number as value
- D
Hash value as key and phone number as the table capacity
Correct answer
CPerson's name as key and phone number as value
Explanation
A phone agenda can use each person's name as a key and the associated phone number as the value.
- A
- 13
How can a hash table implement a word counter?
- A
Use each index as a key and store only distinct sentence lengths
- B
Use each word as a key and its occurrence count as the value
- C
Use each count as a key and store all words in one value
- D
Use each character as a key and store the text length as the value
Correct answer
BUse each word as a key and its occurrence count as the value
Explanation
A word counter stores a word as the key and an integer representing its number of occurrences as the value.
- A
- 14
How can a hash table implement a dictionary?
- A
Use a letter as the key and store all definitions in one value
- B
Use a definition as the key and store the word's index as the value
- C
Use a hash index as the key and discard the original word
- D
Use a word as the key and its definition as the value
Correct answer
DUse a word as the key and its definition as the value
Explanation
A dictionary naturally maps each word, as a key, to its definition, as a value.
- A
- 15
Why must insertion follow the position calculated by the hash function?
- A
The hash function only works before an item is inserted
- B
Insertion otherwise changes the key into a mutable object
- C
Lookup must calculate the same position to find the stored item
- D
The table can store values only in descending index order
Correct answer
CLookup must calculate the same position to find the stored item
Explanation
The same key is hashed during lookup, so the item must have been inserted at the corresponding position.
- A
- 16
What is a collision in a hash table?
- A
A hash function returns a value outside the table
- B
Two different keys produce the same hash or index
- C
A key is inserted before the table is initialized
- D
The same value is assigned to two different keys
Correct answer
BTwo different keys produce the same hash or index
Explanation
A collision occurs when different keys map to the same hash value and therefore the same table position.
- A
- 17
In the phone agenda example, which keys collide?
- A
“John Smith” and “Sarah Plick”
- B
“Sarah Plick” and “Khaled Anas”
- C
“Samy Sterious” and “Khaled Anas”
- D
“John Smith” and “Samy Sterious”
Correct answer
C“Samy Sterious” and “Khaled Anas”
Explanation
Both “Samy Sterious” and “Khaled Anas” hash to index 14.
- A
- 18
What is the basic idea of separate chaining?
- A
Move every colliding entry to the next empty array position
- B
Store multiple colliding entries in a linked list at one slot
- C
Increase the table capacity after every collision
- D
Discard all entries that produce an occupied index
Correct answer
BStore multiple colliding entries in a linked list at one slot
Explanation
Separate chaining allows each slot to contain a linked list holding all entries whose keys map to that slot.
- A
- 19
If three keys hash to index 13, what does separate chaining store at index 13?
- A
Only the last value inserted at index 13
- B
Three separate arrays beginning at index 13
- C
A linked list containing three key/value nodes
- D
A new hash table with exactly three slots
Correct answer
CA linked list containing three key/value nodes
Explanation
The slot at index 13 holds a linked list of length three, with one node for each key/value pair.
- A
- 20
Why must a separate-chaining node store the key as well as the value?
- A
The key allows values to be hashed a second time
- B
The key prevents the linked list from becoming empty
- C
The key identifies the correct value among colliding entries
- D
The key determines how many pointers the node must contain
Correct answer
CThe key identifies the correct value among colliding entries
Explanation
Several values may share a slot, so the stored key is needed to distinguish the requested entry from the others.
- A
- 21
What is the defining principle of open addressing?
- A
All values are stored outside the table in a separate array
- B
Each slot holds at most one entry, and collisions trigger a search for another slot
- C
Each slot contains a linked list of unlimited length
- D
Every collision causes the original key to be deleted
Correct answer
BEach slot holds at most one entry, and collisions trigger a search for another slot
Explanation
Open addressing keeps one entry per slot and probes alternative positions when the original position is occupied.
- A
- 22
What does probing mean in open addressing?
- A
Converting a value back into its original key
- B
Linking every occupied spot to the previous spot
- C
Searching alternative spots for insertion or lookup
- D
Recomputing all hashes without changing the table
Correct answer
CSearching alternative spots for insertion or lookup
Explanation
Probing is the process of searching alternative table positions after a collision.
- A
- 23
How does linear probing resolve a collision?
- A
It creates a linked list at the original position
- B
It checks consecutive positions until an appropriate position is found
- C
It replaces the existing value with the new value
- D
It checks only positions with prime-numbered indices
Correct answer
BIt checks consecutive positions until an appropriate position is found
Explanation
Linear probing moves to the next position repeatedly until it finds an empty slot or the sought entry.
- A
- 24
If a key hashes to 14 and positions 14, 15, and 16 are occupied, where does linear probing insert it?
- A
At position 14 by replacing the existing entry
- B
At the first position in a separate linked list
- C
At position 13
- D
At position 17
Correct answer
DAt position 17
Explanation
Linear probing checks 14, then 15, then 16, and inserts at the first empty position, 17.
- A
- 25
Why are collisions unavoidable when the key space is larger than the number of table slots?
- A
A deterministic hash function must return the same value for every key
- B
A hash function cannot produce an index below the table capacity
- C
The Pigeonhole principle requires different keys to share some slots
- D
The table always stores values before it stores keys
Correct answer
CThe Pigeonhole principle requires different keys to share some slots
Explanation
There are only m possible hash values but potentially infinitely many keys, so different keys must eventually map to the same value.
- A
- 26
Under what condition can a set of keys potentially be mapped without collisions?
- A
The number of keys is greater than the number of table slots
- B
Every key has the same length
- C
The number of keys does not exceed the number of table slots
- D
The table uses separate chaining rather than open addressing
Correct answer
CThe number of keys does not exceed the number of table slots
Explanation
Collision-free mapping is possible when the number of keys is no greater than the number of available slots and a suitable function exists.
- A
- 27
What is a perfect hash function?
- A
A function that automatically doubles the table capacity
- B
A function that accepts only integer keys
- C
A function that always returns the largest valid index
- D
A function that maps a known set of keys to slots without collisions
Correct answer
DA function that maps a known set of keys to slots without collisions
Explanation
Perfect hashing maps a specified set of n keys to available hash values without collisions.
- A
- 28
What property characterizes a good hash function?
- A
It gives different hashes to every possible string
- B
It always uses the maximum possible table index
- C
It maps most keys to a small group of nearby slots
- D
It distributes keys as evenly as possible across hash values
Correct answer
DIt distributes keys as evenly as possible across hash values
Explanation
A good hash function gives hash values with roughly similar probabilities, distributing keys evenly and reducing collisions.
- A
- 29
Why is a hash function that maps most keys to indices 0 through 4 a poor choice for a 30-slot table?
- A
It uses too many possible hash values for the table
- B
It makes every linked list empty after insertion
- C
It prevents the function from being deterministic
- D
It concentrates keys in a small region and creates many collisions
Correct answer
DIt concentrates keys in a small region and creates many collisions
Explanation
Concentrating outputs in a small subset of slots causes many keys to share those slots.
- A
- 30
Why can a very good hash function still produce many collisions in a table with 10 slots and 50 keys?
- A
The table has more slots than keys, causing unused capacity
- B
There is insufficient space to place the keys without sharing slots
- C
A good hash function always maps keys to the same index
- D
Rehashing prevents the function from using all available indices
Correct answer
BThere is insufficient space to place the keys without sharing slots
Explanation
With 50 keys and only 10 slots, collisions are unavoidable; the average number of elements per slot is 5.
- A
- 31
What does rehashing involve?
- A
Deleting all entries that experienced collisions
- B
Replacing every key with its current array index
- C
Creating a larger table and reinserting the existing key/value pairs
- D
Keeping the same table and changing only the stored values
Correct answer
CCreating a larger table and reinserting the existing key/value pairs
Explanation
Rehashing creates a new table with more slots and inserts the previously stored entries into it.
- A
- 32
Why does increasing the number of slots usually reduce collisions?
- A
The hash function becomes automatically deterministic
- B
Each key is converted into a longer value
- C
Keys are distributed over a larger range of possible indices
- D
The number of possible keys becomes smaller
Correct answer
CKeys are distributed over a larger range of possible indices
Explanation
A larger table provides more possible positions, spreading entries across a wider range.
- A
- 33
How is the fullness of a hash table measured?
- A
By the ratio rac{n}{m}, where n is the number of elements and m is the number of slots
- B
By the ratio rac{m}{n}, where m is the number of elements and n is the number of slots
- C
By multiplying the number of keys by the number of collisions
- D
By subtracting the number of occupied slots from the capacity
Correct answer
ABy the ratio rac{n}{m}, where n is the number of elements and m is the number of slots
Explanation
The ratio rac{n}{m} measures the number of stored elements relative to the number of slots.
- A
- 34
What is the maximum possible value of rac{n}{m} in open addressing?
- A
0.5
- B
There is no upper bound
- C
1
- D
m
Correct answer
C1
Explanation
Open addressing allows at most one element per slot, so n cannot exceed m and rac{n}{m} cannot exceed 1.
- A
- 35
Why can rac{n}{m} exceed 1 in separate chaining?
- A
Separate chaining automatically creates more physical slots
- B
The ratio counts only empty slots rather than stored elements
- C
A single slot can contain multiple elements in its linked list
- D
Separate chaining permits keys to occupy negative indices
Correct answer
CA single slot can contain multiple elements in its linked list
Explanation
Because one slot may hold several linked-list nodes, the number of elements can exceed the number of slots.
- A
- 36
What is the load factor?
- A
The ratio of table capacity to key length
- B
The current number of collisions in the largest linked list
- C
The number of values stored in each occupied slot
- D
The maximum allowed value of rac{n}{m} before rehashing
Correct answer
DThe maximum allowed value of rac{n}{m} before rehashing
Explanation
The load factor is the threshold for rac{n}{m} that determines when the table should be rehashed.
- A
- 37
With a load factor of 0.75 and capacity m=100, when does automatic rehashing occur?
- A
When the 76th element is inserted
- B
When the first element is inserted
- C
When the 100th element is inserted
- D
When the 75th element is inserted
Correct answer
AWhen the 76th element is inserted
Explanation
After 75 elements, rac{n}{m}=0.75. Inserting the 76th element makes the ratio exceed 0.75.
- A
- 38
What is the tradeoff when the load factor is reduced?
- A
More collisions but less wasted space
- B
More collisions and more wasted space
- C
Fewer collisions but more wasted space
- D
Slower hashing but fewer table slots
Correct answer
CFewer collisions but more wasted space
Explanation
A lower load factor means more slots relative to elements, reducing collisions but increasing unused space.
- A
- 39
What is the tradeoff when the load factor is increased?
- A
Less wasted space and fewer collisions
- B
Less wasted space but more collisions
- C
More wasted space but fewer collisions
- D
More wasted space and more collisions
Correct answer
BLess wasted space but more collisions
Explanation
A higher load factor keeps the table more compact but leaves fewer available positions, which increases collisions.
- A
- 40
Why does the ASCII-sum hash function give the same hash to “abc” and “acb”?
- A
Addition produces the same total regardless of character order
- B
Both strings are automatically converted into the same key
- C
The function ignores all characters after the first one
- D
The modulo operation sorts characters alphabetically
Correct answer
AAddition produces the same total regardless of character order
Explanation
The sum of the ASCII codes is unchanged by rearranging the characters, so all permutations receive the same hash.
- A
- 41
Why can rehashing fail to reduce collisions when using the simple ASCII-sum function on strings of at most 20 alphabetic characters?
- A
The table cannot contain more than 20 slots
- B
The function always produces a unique index for every string
- C
Rehashing changes keys into values before computing their hashes
- D
The function cannot produce indices beyond the maximum possible ASCII sum
Correct answer
DThe function cannot produce indices beyond the maximum possible ASCII sum
Explanation
The maximum ASCII sum is 20imes122=2440, so indices beyond 2440 can never be reached by this function.
- A
- 42
If a table has 2500 slots but the hash function cannot produce a value above 2440, what happens to slots after index 2440?
- A
They force the hash function to produce negative indices
- B
They automatically store duplicate copies of earlier values
- C
They remain unreachable and therefore cannot reduce collisions
- D
They receive all keys whose hashes are below 2440
Correct answer
CThey remain unreachable and therefore cannot reduce collisions
Explanation
Because the function never returns an index above 2440, those later slots remain empty and provide no additional distribution.
- A
- 43
Besides even distribution, what other property should a good hash function have?
- A
A requirement that every key have a unique hash
- B
Dependence on insertion order
- C
A requirement that all values be immutable
- D
Computational efficiency
Correct answer
DComputational efficiency
Explanation
A good hash function should distribute keys well and be computationally efficient.
- A
- 44
What is the space complexity of a hash table in terms of the number of stored elements n?
- A
O(n2)
- B
O(1)
- C
O(n)
- D
O(rac{1}{n})
Correct answer
CO(n)
Explanation
A hash table stores n nodes and a number of slots proportional to n, giving total space usage of O(n).
- A
- 45
In the separate-chaining implementation, what does each node contain?
- A
A key, a table, and every other node in the array
- B
Only a value and the table's load factor
- C
An index, a capacity, and a new hash function
- D
A key, a value, and a pointer to the next node
Correct answer
DA key, a value, and a pointer to the next node
Explanation
Each linked-list node stores its key, its associated value, and a pointer to the next node.
- A
- 46
What does the linked-list insertion method do in the described implementation?
- A
Replaces the current head without preserving it
- B
Sorts the entire list before adding the node
- C
Traverses to the end and places the node there
- D
Creates a node and places it at the beginning of the list
Correct answer
DCreates a node and places it at the beginning of the list
Explanation
Insertion creates a node whose next pointer references the current head, then makes the new node the head.
- A
- 47
What does linked-list search return when it finds the requested key?
- A
The node containing that key
- B
The hash function itself
- C
The previous list head only
- D
The table capacity
Correct answer
AThe node containing that key
Explanation
The search traverses the list and returns the node whose key matches the requested key; otherwise it returns null.
- A
- 48
What does linked-list deletion do when the requested node is found?
- A
It moves the node to every other slot
- B
It bypasses the node by changing the preceding node's pointer
- C
It deletes the entire linked list at that index
- D
It changes the node's key into the table index
Correct answer
BIt bypasses the node by changing the preceding node's pointer
Explanation
Deletion skips the target node by updating the previous node's next pointer. It returns true if deletion occurs and false otherwise.
- A
- 49
Which set of attributes describes the hash table in the implementation?
- A
Capacity, size, load factor, and an array of linked lists
- B
Key length, value length, pointer count, and a sorting array
- C
Only capacity and a single linked list
- D
Hash value, collision count, and an array of values without slots
Correct answer
ACapacity, size, load factor, and an array of linked lists
Explanation
The implementation maintains capacity, the number of stored elements, a load factor, and an array whose slots contain linked lists.
- A
- 50
What happens during rehashing in the described implementation?
- A
The capacity is halved and all colliding entries are discarded
- B
Only the load factor changes while the old slots remain unchanged
- C
The capacity is doubled, the table is reset, and old entries are reinserted
- D
The values are copied without recomputing their indices
Correct answer
CThe capacity is doubled, the table is reset, and old entries are reinserted
Explanation
The implementation saves the old slots, doubles capacity, creates an empty table, and reinserts every old key/value pair using the new capacity.
- A
- 51
When inserting a key that already exists, what should the hash table do?
- A
Update the existing node's value without increasing the size
- B
Move the key to a random empty slot
- C
Insert a duplicate node and increase the size
- D
Delete the old node and leave the key absent
Correct answer
AUpdate the existing node's value without increasing the size
Explanation
Insertion is defined to add a new pair or update the value associated with an existing key. Only a new node increases the size.
- A
- 52
What should a get operation return when the requested key is absent?
- A
The first value in the table
- B
Null
- C
The table's load factor
- D
The key's original string length
Correct answer
BNull
Explanation
If no node with the requested key exists, the get operation returns null.
- A
- 53
What happens to the hash table size after a successful deletion?
- A
It increases by one
- B
It decreases by one
- C
It remains unchanged
- D
It becomes equal to the capacity
Correct answer
BIt decreases by one
Explanation
The size represents the number of stored nodes, so it is decremented after a successful deletion.
- A
- 54
What is the time complexity of inserting at the beginning of a linked list?
- A
O(n2)
- B
O(l)
- C
O(n)
- D
O(1)
Correct answer
DO(1)
Explanation
Inserting at the head requires changing only a constant number of pointers, so it takes O(1) time.
- A
- 55
If a linked list has length l, what is the time complexity of searching it for a key?
- A
O(1)
- B
O(m) regardless of l
- C
O(n2)
- D
O(l)
Correct answer
DO(l)
Explanation
Searching may require traversing the linked list, so its time complexity is O(l).
- A
- 56
Why is the hash function treated as O(1) with respect to the table size?
- A
Its runtime does not depend on the number of elements in the table
- B
It automatically prevents all collisions
- C
It performs no computation on the key
- D
It always examines every table slot in constant order
Correct answer
AIts runtime does not depend on the number of elements in the table
Explanation
The hash function's runtime is considered independent of the number of stored table elements. Depending on key representation, hashing may otherwise depend on key size.
- A
- 57
What is the best-case time complexity for searching a separate-chaining bucket?
- A
O(n2)
- B
O(m)
- C
O(l) where l=n
- D
O(1)
Correct answer
DO(1)
Explanation
With no collisions, each linked list has length at most one, so searching a bucket takes O(1) time.
- A
- 58
What is the worst-case time complexity for searching a separate-chaining bucket?
- A
O(n)
- B
O(rac{n}{m}) regardless of distribution
- C
O(1)
- D
O(logn)
Correct answer
AO(n)
Explanation
If all n elements land in one bucket, searching that linked list may require examining all n nodes.
- A
- 59
What is the average-case time complexity of lookup with a good hash function and suitable rehashing?
- A
O(mn)
- B
O(1)
- C
O(n)
- D
O(n2)
Correct answer
BO(1)
Explanation
A good distribution keeps bucket lengths bounded on average, so hashing and bucket search take O(1) average time.
- A
- 60
Why is hash-table insertion O(1) amortized even though rehashing can cost O(n)?
- A
Rehashing occurs infrequently, so its cost is spread across many insertions
- B
Rehashing never traverses existing elements
- C
The table performs rehashing after every insertion
- D
Insertion skips the hash function whenever rehashing occurs
Correct answer
ARehashing occurs infrequently, so its cost is spread across many insertions
Explanation
Rehashing is expensive but infrequent. Averaging its cost over a large sequence of insertions gives O(1) amortized insertion time.
- A
- 61
What is the average-case time complexity of deleting a key in the described hash table?
- A
O(m2)
- B
O(1)
- C
O(n2)
- D
O(n) for every deletion
Correct answer
BO(1)
Explanation
Hashing is O(1) and searching the relevant bucket is O(1) on average, so deletion is O(1) on average.
- A
- 62
What is the average-case time complexity of retrieving a value by key?
- A
O(n2)
- B
O(1)
- C
O(logn)
- D
O(n)
Correct answer
BO(1)
Explanation
Retrieving a value requires hashing and searching one bucket, both of which are O(1) on average with a good implementation.
- A
- 63
What is the main advantage of a hash table over a simple array for key/value storage?
- A
It guarantees constant time even in the worst case
- B
It stores values without needing keys
- C
It always uses less space than an array
- D
It can provide average constant-time key operations
Correct answer
DIt can provide average constant-time key operations
Explanation
Hash tables provide average O(1) insertion, lookup, retrieval, and deletion, making them time-efficient for associative storage.
- A
- 64
What is the time complexity of the straightforward two-sum approach that tests all pairs?
- A
O(logn)
- B
O(1)
- C
O(n)
- D
O(n2)
Correct answer
DO(n2)
Explanation
Checking all pairs of elements requires quadratic time, or O(n2).
- A
- 65
How does a hash table improve the two-sum problem?
- A
It stores only the largest element and ignores previous indices
- B
It stores seen elements and their indices, allowing complements to be found in O(n) time
- C
It sorts the array and checks every pair in O(n2) time
- D
It replaces every array element with its hash and requires no lookup
Correct answer
BIt stores seen elements and their indices, allowing complements to be found in O(n) time
Explanation
For each element x, the algorithm checks whether k−x has already been seen in the hash table, producing an overall O(n) solution.
- A
- 66
What information is stored for each element in the hash-table solution to two-sum?
- A
Only the complement and the table capacity
- B
The element and every pair tested previously
- C
Only the element's hash and its value
- D
The element and the index where it was seen
Correct answer
DThe element and the index where it was seen
Explanation
The method stores each element together with its array index so that matching indices can be returned.
- A
- 67
What uniqueness rule applies to hash-table keys and values?
- A
Neither keys nor values may ever be repeated
- B
Values must be unique, but keys may be duplicated
- C
Keys must be unique, but different keys may have equal values
- D
Both keys and values must always be unique
Correct answer
CKeys must be unique, but different keys may have equal values
Explanation
A hash table cannot contain the same key twice as separate entries, but multiple keys can legitimately map to the same value.
- A
- 68
Why must a hash-table key be immutable?
- A
Changing the key could change its hash and make the stored entry unreachable
- B
Immutable keys require more linked-list nodes than mutable keys
- C
Changing a key automatically increases the table's capacity
- D
Mutable keys cannot be stored as values in any data structure
Correct answer
AChanging the key could change its hash and make the stored entry unreachable
Explanation
If a key changes after insertion, hashing it again may produce a different index, violating the requirement that the same key always produce the same hash.
- A
- 69
Which object is suitable as a hash-table key according to the material?
- A
An array
- B
A mutable object whose contents change after insertion
- C
A set
- D
A string
Correct answer
DA string
Explanation
Strings, integers, and tuples are examples of immutable types suitable for keys, whereas arrays and sets are mutable.
- A
- 70
Which object may safely be used as a hash-table value even if it is mutable?
- A
An array
- B
A set used as the key
- C
A mutable key whose hash changes
- D
An object that must determine its own table index
Correct answer
AAn array
Explanation
Values are not hashed, so they can be mutable objects such as arrays, hash tables, or other objects.
- A
- 71
Why is a mutable array unsuitable as a hash-table key?
- A
Arrays always produce the same hash as every other key
- B
Mutating it may cause the hash function to return a different hash later
- C
Mutable arrays can be stored only in linked lists
- D
Arrays cannot contain values of more than one type
Correct answer
BMutating it may cause the hash function to return a different hash later
Explanation
After mutation, the array's contents differ, so hashing it may produce a different result from the hash computed during insertion.
- A
- 72
Which statement best summarizes the requirements for a hash-table key?
- A
It must be a numerical value and larger than the table capacity
- B
It must be mutable and duplicated when necessary
- C
It must be unique and immutable
- D
It must be unique only when its associated value is mutable
Correct answer
CIt must be unique and immutable
Explanation
Keys must be unique identifiers and immutable so that their hashes remain stable.
- A
- 73
Which application beyond hash-table implementation is mentioned for hashing?
- A
Compressing every array into a single value
- B
Balancing binary trees
- C
Cryptography
- D
Sorting linked lists
Correct answer
CCryptography
Explanation
The material notes that hashing also has applications in cryptography.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.