1. Home
  2. Explore
  3. Computer Science
  4. Hashing and Hash Tables

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Hashing and Hash Tables

  • 73Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Hashing Basics

What is hashing?

73 questions

  1. 1

    What is hashing?

    1. A

      Storing every key in a linked list

    2. B

      Converting a key into a fixed-size value

    3. C

      Sorting all values into ascending order

    4. D

      Comparing each key with every other key

    Correct answer
    B

    Converting a key into a fixed-size value

    Explanation

    Hashing is the process of converting a key into a fixed-size value called a hash.

  2. 2

    What is the result produced by a hashing process called?

    1. A

      A hash

    2. B

      A node

    3. C

      A capacity

    4. D

      A pointer

    Correct answer
    A

    A hash

    Explanation

    The fixed-size value produced by hashing a key is called a hash.

  3. 3

    What does a hash function do?

    1. A

      It takes a key, performs computations, and returns its hash

    2. B

      It traverses every element before returning an index

    3. C

      It sorts all keys according to their lengths

    4. D

      It takes a value and stores it directly in the first slot

    Correct answer
    A

    It 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.

  4. 4

    Why must a hash function be deterministic?

    1. A

      The function must use the same amount of memory as the table

    2. B

      The hash table must always contain the same number of slots

    3. C

      The same key must produce the same hash during insertion and lookup

    4. D

      Every different key must produce a different hash

    Correct answer
    C

    The 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.

  5. 5

    For a hash table with mmm slots, what range of indices should a hash function produce?

    1. A

      From −m-m−m through mmm

    2. B

      From 000 through mmm

    3. C

      From 000 through m−1m-1m−1

    4. D

      From 111 through mmm

    Correct answer
    C

    From 000 through m−1m-1m−1

    Explanation

    A table with mmm slots has valid indices from 000 to m−1m-1m−1.

  6. 6

    In the example hash function, how is the hash of a string calculated?

    1. A

      Count the characters and add the table capacity

    2. B

      Sum the ASCII codes and take the result modulo mmm

    3. C

      Multiply the ASCII codes and divide by the string length

    4. D

      Sort the characters and use the first character's code

    Correct answer
    B

    Sum the ASCII codes and take the result modulo mmm

    Explanation

    The example function adds the ASCII code of each character and returns the sum modulo mmm.

  7. 7

    Using the example function, what hash is obtained for “inside code” when m=30m=30m=30 and the ASCII sum is 107910791079?

    1. A

      191919

    2. B

      292929

    3. C

      303030

    4. D

      107910791079

    Correct answer
    B

    292929

    Explanation

    Since 1079 mod 30 = 29, the key maps to index 292929.

  8. 8

    What is the main advantage of using hashing to locate an item?

    1. A

      It eliminates the need to store the item associated with a key

    2. B

      It guarantees that every key receives a unique position

    3. C

      It ensures that the table requires no additional memory

    4. D

      It can calculate a likely position without scanning all earlier items

    Correct answer
    D

    It 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.

  9. 9

    What does a hash table store?

    1. A

      Only numerical indices

    2. B

      Only sorted keys

    3. C

      A sequence of unassociated values

    4. D

      Key/value pairs

    Correct answer
    D

    Key/value pairs

    Explanation

    A hash table is a data structure that stores key/value pairs.

  10. 10

    How is a value located in a hash table?

    1. A

      The table scans every value until the key is found

    2. B

      The key is converted into a linked-list pointer

    3. C

      The key is hashed to calculate the value's array index

    4. D

      The value is always stored at the key's character count

    Correct answer
    C

    The 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.

  11. 11

    In the library analogy, what corresponds to the value in a key/value pair?

    1. A

      The book itself

    2. B

      The library position

    3. C

      The hash function

    4. D

      The book's title

    Correct answer
    A

    The book itself

    Explanation

    The book title is the key, the book itself is the value, and the library represents the array of slots.

  12. 12

    Which mapping correctly represents a phone agenda implemented with a hash table?

    1. A

      Array index as key and person's name as value

    2. B

      Phone number as key and person's name as value only during lookup

    3. C

      Person's name as key and phone number as value

    4. D

      Hash value as key and phone number as the table capacity

    Correct answer
    C

    Person'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.

  13. 13

    How can a hash table implement a word counter?

    1. A

      Use each index as a key and store only distinct sentence lengths

    2. B

      Use each word as a key and its occurrence count as the value

    3. C

      Use each count as a key and store all words in one value

    4. D

      Use each character as a key and store the text length as the value

    Correct answer
    B

    Use 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.

  14. 14

    How can a hash table implement a dictionary?

    1. A

      Use a letter as the key and store all definitions in one value

    2. B

      Use a definition as the key and store the word's index as the value

    3. C

      Use a hash index as the key and discard the original word

    4. D

      Use a word as the key and its definition as the value

    Correct answer
    D

    Use 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.

  15. 15

    Why must insertion follow the position calculated by the hash function?

    1. A

      The hash function only works before an item is inserted

    2. B

      Insertion otherwise changes the key into a mutable object

    3. C

      Lookup must calculate the same position to find the stored item

    4. D

      The table can store values only in descending index order

    Correct answer
    C

    Lookup 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.

  16. 16

    What is a collision in a hash table?

    1. A

      A hash function returns a value outside the table

    2. B

      Two different keys produce the same hash or index

    3. C

      A key is inserted before the table is initialized

    4. D

      The same value is assigned to two different keys

    Correct answer
    B

    Two 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.

  17. 17

    In the phone agenda example, which keys collide?

    1. A

      “John Smith” and “Sarah Plick”

    2. B

      “Sarah Plick” and “Khaled Anas”

    3. C

      “Samy Sterious” and “Khaled Anas”

    4. D

      “John Smith” and “Samy Sterious”

    Correct answer
    C

    “Samy Sterious” and “Khaled Anas”

    Explanation

    Both “Samy Sterious” and “Khaled Anas” hash to index 141414.

  18. 18

    What is the basic idea of separate chaining?

    1. A

      Move every colliding entry to the next empty array position

    2. B

      Store multiple colliding entries in a linked list at one slot

    3. C

      Increase the table capacity after every collision

    4. D

      Discard all entries that produce an occupied index

    Correct answer
    B

    Store 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.

  19. 19

    If three keys hash to index 131313, what does separate chaining store at index 131313?

    1. A

      Only the last value inserted at index 131313

    2. B

      Three separate arrays beginning at index 131313

    3. C

      A linked list containing three key/value nodes

    4. D

      A new hash table with exactly three slots

    Correct answer
    C

    A linked list containing three key/value nodes

    Explanation

    The slot at index 131313 holds a linked list of length three, with one node for each key/value pair.

  20. 20

    Why must a separate-chaining node store the key as well as the value?

    1. A

      The key allows values to be hashed a second time

    2. B

      The key prevents the linked list from becoming empty

    3. C

      The key identifies the correct value among colliding entries

    4. D

      The key determines how many pointers the node must contain

    Correct answer
    C

    The 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.

  21. 21

    What is the defining principle of open addressing?

    1. A

      All values are stored outside the table in a separate array

    2. B

      Each slot holds at most one entry, and collisions trigger a search for another slot

    3. C

      Each slot contains a linked list of unlimited length

    4. D

      Every collision causes the original key to be deleted

    Correct answer
    B

    Each 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.

  22. 22

    What does probing mean in open addressing?

    1. A

      Converting a value back into its original key

    2. B

      Linking every occupied spot to the previous spot

    3. C

      Searching alternative spots for insertion or lookup

    4. D

      Recomputing all hashes without changing the table

    Correct answer
    C

    Searching alternative spots for insertion or lookup

    Explanation

    Probing is the process of searching alternative table positions after a collision.

  23. 23

    How does linear probing resolve a collision?

    1. A

      It creates a linked list at the original position

    2. B

      It checks consecutive positions until an appropriate position is found

    3. C

      It replaces the existing value with the new value

    4. D

      It checks only positions with prime-numbered indices

    Correct answer
    B

    It 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.

  24. 24

    If a key hashes to 141414 and positions 141414, 151515, and 161616 are occupied, where does linear probing insert it?

    1. A

      At position 141414 by replacing the existing entry

    2. B

      At the first position in a separate linked list

    3. C

      At position 131313

    4. D

      At position 171717

    Correct answer
    D

    At position 171717

    Explanation

    Linear probing checks 141414, then 151515, then 161616, and inserts at the first empty position, 171717.

  25. 25

    Why are collisions unavoidable when the key space is larger than the number of table slots?

    1. A

      A deterministic hash function must return the same value for every key

    2. B

      A hash function cannot produce an index below the table capacity

    3. C

      The Pigeonhole principle requires different keys to share some slots

    4. D

      The table always stores values before it stores keys

    Correct answer
    C

    The Pigeonhole principle requires different keys to share some slots

    Explanation

    There are only mmm possible hash values but potentially infinitely many keys, so different keys must eventually map to the same value.

  26. 26

    Under what condition can a set of keys potentially be mapped without collisions?

    1. A

      The number of keys is greater than the number of table slots

    2. B

      Every key has the same length

    3. C

      The number of keys does not exceed the number of table slots

    4. D

      The table uses separate chaining rather than open addressing

    Correct answer
    C

    The 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.

  27. 27

    What is a perfect hash function?

    1. A

      A function that automatically doubles the table capacity

    2. B

      A function that accepts only integer keys

    3. C

      A function that always returns the largest valid index

    4. D

      A function that maps a known set of keys to slots without collisions

    Correct answer
    D

    A function that maps a known set of keys to slots without collisions

    Explanation

    Perfect hashing maps a specified set of nnn keys to available hash values without collisions.

  28. 28

    What property characterizes a good hash function?

    1. A

      It gives different hashes to every possible string

    2. B

      It always uses the maximum possible table index

    3. C

      It maps most keys to a small group of nearby slots

    4. D

      It distributes keys as evenly as possible across hash values

    Correct answer
    D

    It 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.

  29. 29

    Why is a hash function that maps most keys to indices 000 through 444 a poor choice for a 303030-slot table?

    1. A

      It uses too many possible hash values for the table

    2. B

      It makes every linked list empty after insertion

    3. C

      It prevents the function from being deterministic

    4. D

      It concentrates keys in a small region and creates many collisions

    Correct answer
    D

    It 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.

  30. 30

    Why can a very good hash function still produce many collisions in a table with 101010 slots and 505050 keys?

    1. A

      The table has more slots than keys, causing unused capacity

    2. B

      There is insufficient space to place the keys without sharing slots

    3. C

      A good hash function always maps keys to the same index

    4. D

      Rehashing prevents the function from using all available indices

    Correct answer
    B

    There is insufficient space to place the keys without sharing slots

    Explanation

    With 505050 keys and only 101010 slots, collisions are unavoidable; the average number of elements per slot is 555.

  31. 31

    What does rehashing involve?

    1. A

      Deleting all entries that experienced collisions

    2. B

      Replacing every key with its current array index

    3. C

      Creating a larger table and reinserting the existing key/value pairs

    4. D

      Keeping the same table and changing only the stored values

    Correct answer
    C

    Creating 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.

  32. 32

    Why does increasing the number of slots usually reduce collisions?

    1. A

      The hash function becomes automatically deterministic

    2. B

      Each key is converted into a longer value

    3. C

      Keys are distributed over a larger range of possible indices

    4. D

      The number of possible keys becomes smaller

    Correct answer
    C

    Keys are distributed over a larger range of possible indices

    Explanation

    A larger table provides more possible positions, spreading entries across a wider range.

  33. 33

    How is the fullness of a hash table measured?

    1. A

      By the ratio rac{n}{m}, where nnn is the number of elements and mmm is the number of slots

    2. B

      By the ratio rac{m}{n}, where mmm is the number of elements and nnn is the number of slots

    3. C

      By multiplying the number of keys by the number of collisions

    4. D

      By subtracting the number of occupied slots from the capacity

    Correct answer
    A

    By the ratio rac{n}{m}, where nnn is the number of elements and mmm is the number of slots

    Explanation

    The ratio rac{n}{m} measures the number of stored elements relative to the number of slots.

  34. 34

    What is the maximum possible value of rac{n}{m} in open addressing?

    1. A

      0.50.50.5

    2. B

      There is no upper bound

    3. C

      111

    4. D

      mmm

    Correct answer
    C

    111

    Explanation

    Open addressing allows at most one element per slot, so nnn cannot exceed mmm and rac{n}{m} cannot exceed 111.

  35. 35

    Why can rac{n}{m} exceed 111 in separate chaining?

    1. A

      Separate chaining automatically creates more physical slots

    2. B

      The ratio counts only empty slots rather than stored elements

    3. C

      A single slot can contain multiple elements in its linked list

    4. D

      Separate chaining permits keys to occupy negative indices

    Correct answer
    C

    A 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.

  36. 36

    What is the load factor?

    1. A

      The ratio of table capacity to key length

    2. B

      The current number of collisions in the largest linked list

    3. C

      The number of values stored in each occupied slot

    4. D

      The maximum allowed value of rac{n}{m} before rehashing

    Correct answer
    D

    The 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.

  37. 37

    With a load factor of 0.750.750.75 and capacity m=100m=100m=100, when does automatic rehashing occur?

    1. A

      When the 767676th element is inserted

    2. B

      When the first element is inserted

    3. C

      When the 100100100th element is inserted

    4. D

      When the 757575th element is inserted

    Correct answer
    A

    When the 767676th element is inserted

    Explanation

    After 757575 elements, rac{n}{m}=0.75. Inserting the 767676th element makes the ratio exceed 0.750.750.75.

  38. 38

    What is the tradeoff when the load factor is reduced?

    1. A

      More collisions but less wasted space

    2. B

      More collisions and more wasted space

    3. C

      Fewer collisions but more wasted space

    4. D

      Slower hashing but fewer table slots

    Correct answer
    C

    Fewer collisions but more wasted space

    Explanation

    A lower load factor means more slots relative to elements, reducing collisions but increasing unused space.

  39. 39

    What is the tradeoff when the load factor is increased?

    1. A

      Less wasted space and fewer collisions

    2. B

      Less wasted space but more collisions

    3. C

      More wasted space but fewer collisions

    4. D

      More wasted space and more collisions

    Correct answer
    B

    Less wasted space but more collisions

    Explanation

    A higher load factor keeps the table more compact but leaves fewer available positions, which increases collisions.

  40. 40

    Why does the ASCII-sum hash function give the same hash to “abc” and “acb”?

    1. A

      Addition produces the same total regardless of character order

    2. B

      Both strings are automatically converted into the same key

    3. C

      The function ignores all characters after the first one

    4. D

      The modulo operation sorts characters alphabetically

    Correct answer
    A

    Addition 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.

  41. 41

    Why can rehashing fail to reduce collisions when using the simple ASCII-sum function on strings of at most 202020 alphabetic characters?

    1. A

      The table cannot contain more than 202020 slots

    2. B

      The function always produces a unique index for every string

    3. C

      Rehashing changes keys into values before computing their hashes

    4. D

      The function cannot produce indices beyond the maximum possible ASCII sum

    Correct answer
    D

    The function cannot produce indices beyond the maximum possible ASCII sum

    Explanation

    The maximum ASCII sum is 20imes122=244020 imes 122 = 244020imes122=2440, so indices beyond 244024402440 can never be reached by this function.

  42. 42

    If a table has 250025002500 slots but the hash function cannot produce a value above 244024402440, what happens to slots after index 244024402440?

    1. A

      They force the hash function to produce negative indices

    2. B

      They automatically store duplicate copies of earlier values

    3. C

      They remain unreachable and therefore cannot reduce collisions

    4. D

      They receive all keys whose hashes are below 244024402440

    Correct answer
    C

    They remain unreachable and therefore cannot reduce collisions

    Explanation

    Because the function never returns an index above 244024402440, those later slots remain empty and provide no additional distribution.

  43. 43

    Besides even distribution, what other property should a good hash function have?

    1. A

      A requirement that every key have a unique hash

    2. B

      Dependence on insertion order

    3. C

      A requirement that all values be immutable

    4. D

      Computational efficiency

    Correct answer
    D

    Computational efficiency

    Explanation

    A good hash function should distribute keys well and be computationally efficient.

  44. 44

    What is the space complexity of a hash table in terms of the number of stored elements nnn?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O( rac{1}{n})

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    A hash table stores nnn nodes and a number of slots proportional to nnn, giving total space usage of O(n)O(n)O(n).

  45. 45

    In the separate-chaining implementation, what does each node contain?

    1. A

      A key, a table, and every other node in the array

    2. B

      Only a value and the table's load factor

    3. C

      An index, a capacity, and a new hash function

    4. D

      A key, a value, and a pointer to the next node

    Correct answer
    D

    A 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.

  46. 46

    What does the linked-list insertion method do in the described implementation?

    1. A

      Replaces the current head without preserving it

    2. B

      Sorts the entire list before adding the node

    3. C

      Traverses to the end and places the node there

    4. D

      Creates a node and places it at the beginning of the list

    Correct answer
    D

    Creates 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.

  47. 47

    What does linked-list search return when it finds the requested key?

    1. A

      The node containing that key

    2. B

      The hash function itself

    3. C

      The previous list head only

    4. D

      The table capacity

    Correct answer
    A

    The node containing that key

    Explanation

    The search traverses the list and returns the node whose key matches the requested key; otherwise it returns null.

  48. 48

    What does linked-list deletion do when the requested node is found?

    1. A

      It moves the node to every other slot

    2. B

      It bypasses the node by changing the preceding node's pointer

    3. C

      It deletes the entire linked list at that index

    4. D

      It changes the node's key into the table index

    Correct answer
    B

    It 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.

  49. 49

    Which set of attributes describes the hash table in the implementation?

    1. A

      Capacity, size, load factor, and an array of linked lists

    2. B

      Key length, value length, pointer count, and a sorting array

    3. C

      Only capacity and a single linked list

    4. D

      Hash value, collision count, and an array of values without slots

    Correct answer
    A

    Capacity, 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.

  50. 50

    What happens during rehashing in the described implementation?

    1. A

      The capacity is halved and all colliding entries are discarded

    2. B

      Only the load factor changes while the old slots remain unchanged

    3. C

      The capacity is doubled, the table is reset, and old entries are reinserted

    4. D

      The values are copied without recomputing their indices

    Correct answer
    C

    The 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.

  51. 51

    When inserting a key that already exists, what should the hash table do?

    1. A

      Update the existing node's value without increasing the size

    2. B

      Move the key to a random empty slot

    3. C

      Insert a duplicate node and increase the size

    4. D

      Delete the old node and leave the key absent

    Correct answer
    A

    Update 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.

  52. 52

    What should a get operation return when the requested key is absent?

    1. A

      The first value in the table

    2. B

      Null

    3. C

      The table's load factor

    4. D

      The key's original string length

    Correct answer
    B

    Null

    Explanation

    If no node with the requested key exists, the get operation returns null.

  53. 53

    What happens to the hash table size after a successful deletion?

    1. A

      It increases by one

    2. B

      It decreases by one

    3. C

      It remains unchanged

    4. D

      It becomes equal to the capacity

    Correct answer
    B

    It decreases by one

    Explanation

    The size represents the number of stored nodes, so it is decremented after a successful deletion.

  54. 54

    What is the time complexity of inserting at the beginning of a linked list?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(l)O(l)O(l)

    3. C

      O(n)O(n)O(n)

    4. D

      O(1)O(1)O(1)

    Correct answer
    D

    O(1)O(1)O(1)

    Explanation

    Inserting at the head requires changing only a constant number of pointers, so it takes O(1)O(1)O(1) time.

  55. 55

    If a linked list has length lll, what is the time complexity of searching it for a key?

    1. A

      O(1)O(1)O(1)

    2. B

      O(m)O(m)O(m) regardless of lll

    3. C

      O(n2)O(n^2)O(n2)

    4. D

      O(l)O(l)O(l)

    Correct answer
    D

    O(l)O(l)O(l)

    Explanation

    Searching may require traversing the linked list, so its time complexity is O(l)O(l)O(l).

  56. 56

    Why is the hash function treated as O(1)O(1)O(1) with respect to the table size?

    1. A

      Its runtime does not depend on the number of elements in the table

    2. B

      It automatically prevents all collisions

    3. C

      It performs no computation on the key

    4. D

      It always examines every table slot in constant order

    Correct answer
    A

    Its 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.

  57. 57

    What is the best-case time complexity for searching a separate-chaining bucket?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(m)O(m)O(m)

    3. C

      O(l)O(l)O(l) where l=nl=nl=n

    4. D

      O(1)O(1)O(1)

    Correct answer
    D

    O(1)O(1)O(1)

    Explanation

    With no collisions, each linked list has length at most one, so searching a bucket takes O(1)O(1)O(1) time.

  58. 58

    What is the worst-case time complexity for searching a separate-chaining bucket?

    1. A

      O(n)O(n)O(n)

    2. B

      O( rac{n}{m}) regardless of distribution

    3. C

      O(1)O(1)O(1)

    4. D

      O(logn)O(\\log n)O(logn)

    Correct answer
    A

    O(n)O(n)O(n)

    Explanation

    If all nnn elements land in one bucket, searching that linked list may require examining all nnn nodes.

  59. 59

    What is the average-case time complexity of lookup with a good hash function and suitable rehashing?

    1. A

      O(mn)O(mn)O(mn)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    B

    O(1)O(1)O(1)

    Explanation

    A good distribution keeps bucket lengths bounded on average, so hashing and bucket search take O(1)O(1)O(1) average time.

  60. 60

    Why is hash-table insertion O(1)O(1)O(1) amortized even though rehashing can cost O(n)O(n)O(n)?

    1. A

      Rehashing occurs infrequently, so its cost is spread across many insertions

    2. B

      Rehashing never traverses existing elements

    3. C

      The table performs rehashing after every insertion

    4. D

      Insertion skips the hash function whenever rehashing occurs

    Correct answer
    A

    Rehashing 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)O(1)O(1) amortized insertion time.

  61. 61

    What is the average-case time complexity of deleting a key in the described hash table?

    1. A

      O(m2)O(m^2)O(m2)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n2)O(n^2)O(n2)

    4. D

      O(n)O(n)O(n) for every deletion

    Correct answer
    B

    O(1)O(1)O(1)

    Explanation

    Hashing is O(1)O(1)O(1) and searching the relevant bucket is O(1)O(1)O(1) on average, so deletion is O(1)O(1)O(1) on average.

  62. 62

    What is the average-case time complexity of retrieving a value by key?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(1)O(1)O(1)

    3. C

      O(log⁡n)O(\log n)O(logn)

    4. D

      O(n)O(n)O(n)

    Correct answer
    B

    O(1)O(1)O(1)

    Explanation

    Retrieving a value requires hashing and searching one bucket, both of which are O(1)O(1)O(1) on average with a good implementation.

  63. 63

    What is the main advantage of a hash table over a simple array for key/value storage?

    1. A

      It guarantees constant time even in the worst case

    2. B

      It stores values without needing keys

    3. C

      It always uses less space than an array

    4. D

      It can provide average constant-time key operations

    Correct answer
    D

    It can provide average constant-time key operations

    Explanation

    Hash tables provide average O(1)O(1)O(1) insertion, lookup, retrieval, and deletion, making them time-efficient for associative storage.

  64. 64

    What is the time complexity of the straightforward two-sum approach that tests all pairs?

    1. A

      O(log⁡n)O(\log n)O(logn)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    D

    O(n2)O(n^2)O(n2)

    Explanation

    Checking all pairs of elements requires quadratic time, or O(n2)O(n^2)O(n2).

  65. 65

    How does a hash table improve the two-sum problem?

    1. A

      It stores only the largest element and ignores previous indices

    2. B

      It stores seen elements and their indices, allowing complements to be found in O(n)O(n)O(n) time

    3. C

      It sorts the array and checks every pair in O(n2)O(n^2)O(n2) time

    4. D

      It replaces every array element with its hash and requires no lookup

    Correct answer
    B

    It stores seen elements and their indices, allowing complements to be found in O(n)O(n)O(n) time

    Explanation

    For each element xxx, the algorithm checks whether k−xk-xk−x has already been seen in the hash table, producing an overall O(n)O(n)O(n) solution.

  66. 66

    What information is stored for each element in the hash-table solution to two-sum?

    1. A

      Only the complement and the table capacity

    2. B

      The element and every pair tested previously

    3. C

      Only the element's hash and its value

    4. D

      The element and the index where it was seen

    Correct answer
    D

    The 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.

  67. 67

    What uniqueness rule applies to hash-table keys and values?

    1. A

      Neither keys nor values may ever be repeated

    2. B

      Values must be unique, but keys may be duplicated

    3. C

      Keys must be unique, but different keys may have equal values

    4. D

      Both keys and values must always be unique

    Correct answer
    C

    Keys 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.

  68. 68

    Why must a hash-table key be immutable?

    1. A

      Changing the key could change its hash and make the stored entry unreachable

    2. B

      Immutable keys require more linked-list nodes than mutable keys

    3. C

      Changing a key automatically increases the table's capacity

    4. D

      Mutable keys cannot be stored as values in any data structure

    Correct answer
    A

    Changing 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.

  69. 69

    Which object is suitable as a hash-table key according to the material?

    1. A

      An array

    2. B

      A mutable object whose contents change after insertion

    3. C

      A set

    4. D

      A string

    Correct answer
    D

    A string

    Explanation

    Strings, integers, and tuples are examples of immutable types suitable for keys, whereas arrays and sets are mutable.

  70. 70

    Which object may safely be used as a hash-table value even if it is mutable?

    1. A

      An array

    2. B

      A set used as the key

    3. C

      A mutable key whose hash changes

    4. D

      An object that must determine its own table index

    Correct answer
    A

    An array

    Explanation

    Values are not hashed, so they can be mutable objects such as arrays, hash tables, or other objects.

  71. 71

    Why is a mutable array unsuitable as a hash-table key?

    1. A

      Arrays always produce the same hash as every other key

    2. B

      Mutating it may cause the hash function to return a different hash later

    3. C

      Mutable arrays can be stored only in linked lists

    4. D

      Arrays cannot contain values of more than one type

    Correct answer
    B

    Mutating 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.

  72. 72

    Which statement best summarizes the requirements for a hash-table key?

    1. A

      It must be a numerical value and larger than the table capacity

    2. B

      It must be mutable and duplicated when necessary

    3. C

      It must be unique and immutable

    4. D

      It must be unique only when its associated value is mutable

    Correct answer
    C

    It must be unique and immutable

    Explanation

    Keys must be unique identifiers and immutable so that their hashes remain stable.

  73. 73

    Which application beyond hash-table implementation is mentioned for hashing?

    1. A

      Compressing every array into a single value

    2. B

      Balancing binary trees

    3. C

      Cryptography

    4. D

      Sorting linked lists

    Correct answer
    C

    Cryptography

    Explanation

    The material notes that hashing also has applications in cryptography.

Question 1 of 73
Question 1 of 73

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent