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

  • 83Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Hashing Fundamentals
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is linear search?

Tap to flipClick to flipspace

A search method that examines items one by one until the target is found.

Or swipe the card

83 cards

  1. What is linear search?

    A search method that examines items one by one until the target is found.

  2. What is hashing?

    The process of converting a key into a fixed-size value.

  3. What is a hash?

    The fixed-size value produced by hashing a key.

  4. Why is hashing useful in a data structure?

    It allows a key to be converted directly into the position of its associated value, avoiding a search through all preceding elements.

  5. What is a hash table?

    A data structure that stores key/value pairs and uses hashing to determine the array index of a value.

  6. How does the library analogy represent a hash table?

    The book title is the key, the book is the value, the library is the array, and a library position is an array index.

  7. What condition must be followed when inserting an item into a hash table?

    The item must be inserted at the position determined by hashing its key.

  8. What is a hash function?

    A function that takes a key as input, performs computations, and returns the key's hash.

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

    An integer from 000 through māˆ’1m-1māˆ’1.

  10. What does it mean for a hash function to be deterministic?

    The same key always produces the same hash.

  11. What simple hash function can be constructed for strings?

    Add the ASCII codes of all characters in the string and return the sum modulo mmm.

  12. Using the simple ASCII-sum hash function, what is the hash of "inside code" when m=30m=30m=30 and the ASCII sum is 107910791079?

    292929, because 1079ā€Šmodā€Š30=291079 \bmod 30 = 291079mod30=29.

  13. What is the main purpose of taking a hash value modulo mmm?

    To ensure that the result is a valid table index from 000 to māˆ’1m-1māˆ’1.

  14. What property should a good hash function have regarding the distribution of hash values?

    It should distribute keys as evenly as possible across all possible hash values.

  15. Why is computational efficiency important for a hash function?

    A hash function should calculate indices quickly so that hash-table operations remain efficient.

  16. Why is the ASCII-sum hash function insensitive to character order?

    Addition produces the same sum for any permutation of the same characters.

  17. What problem results from giving all permutations of a set of letters the same hash?

    It creates unnecessary collisions among different order-sensitive strings.

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

    The function cannot produce values above 244024402440, so slots beyond index 244024402440 remain unreachable.

  19. What is a phone agenda implemented with a hash table?

    The person's name is the key, and the phone number is the associated value.

  20. How can a hash table implement a word counter?

    Store each word as a key and its occurrence count as an integer value.

  21. How can a hash table implement a dictionary?

    Store each word as a key and its definition as the value.

  22. Besides hash tables, what is another application of hashing mentioned in the material?

    Cryptography.

  23. How does a hash table solve the two-sum problem efficiently?

    Store each array element and its index; for an element xxx, check whether kāˆ’xk-xkāˆ’x has already been stored.

  24. What is the time complexity of the hash-table approach to the two-sum problem?

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

  25. What is a collision in a hash table?

    A situation in which two different keys produce the same hash or index.

  26. Why are collisions unavoidable in most hash tables?

    A hash table has only mmm possible hash values, while the set of possible keys can be infinite.

  27. What is separate chaining?

    A collision-handling method in which each slot stores a linked list of key/value pairs with that slot's hash.

  28. How does separate chaining handle three keys hashing to index 131313?

    The slot at index 131313 contains a linked list of length 333, with one key/value pair in each node.

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

    The key identifies which value belongs to the requested entry when several values share a slot.

  30. How is a value found using separate chaining?

    Hash the key, access the corresponding linked list, and search its nodes for the matching key.

  31. What is open addressing?

    A collision-handling method that keeps at most one element per slot and searches for an alternative slot when the original is occupied.

  32. What is probing?

    Searching alternative table positions for an item when its original hash index is occupied.

  33. What is linear probing?

    A probing method that checks consecutive slots until it finds the required item or an empty slot.

  34. Why must open addressing also store each key with its value?

    The key is needed to determine whether a probed entry is the one being searched for.

  35. What is perfect hashing?

    Mapping a fixed set of nnn keys to table positions with no collisions.

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

    When the number of keys does not exceed the table size and a suitable hash function is chosen.

  37. What is rehashing?

    Creating a new table with more slots and reinserting all existing key/value pairs into it.

  38. Why does rehashing generally reduce collisions?

    It distributes the keys over a larger range of table positions.

  39. What is the load factor?

    The maximum allowed value of the ratio n/mn/mn/m before rehashing occurs, where nnn is the number of elements and mmm is the number of slots.

  40. What does a load factor of 0.750.750.75 mean?

    Rehash when the ratio n/mn/mn/m exceeds 0.750.750.75.

  41. How is the ratio n/mn/mn/m interpreted in open addressing?

    It ranges from 000 to 111, because each slot holds at most one element.

  42. How is the ratio n/mn/mn/m interpreted in separate chaining?

    It can exceed 111 because multiple elements may occupy the same slot's linked list.

  43. What happens when the load factor is decreased?

    The table uses more slots, causing fewer collisions but more wasted space.

  44. What happens when the load factor is increased?

    The table uses fewer slots, saving space but generally causing more collisions and slower operations.

  45. Why might a hash table be rehashed when it becomes too empty?

    To reduce the number of unused slots and save space.

  46. What happens when 505050 keys are inserted into a table with 101010 slots, even with a good hash function?

    There will be many collisions, with an average of 555 elements per slot.

  47. What are the main components of a separate-chaining node?

    A key, a value, and a pointer to the next node in the linked list.

  48. What does the linked-list structure used in the implementation contain?

    A reference to its head node.

  49. How is a node inserted at the beginning of a linked list?

    Create a node whose next pointer is the current head, then make the new node the head.

  50. How does linked-list search work in the implementation?

    Traverse the nodes and return the node whose key matches the input; return null if none matches.

  51. How does linked-list deletion work in the implementation?

    Find the node preceding the target, bypass the target by changing the next pointer, and return whether deletion occurred.

  52. What four pieces of state does the hash table contain?

    Capacity, size, load factor, and an array of slots.

  53. What does the capacity of a hash table represent?

    The number of slots in the table.

  54. What does the size of a hash table represent?

    The number of elements or key/value pairs currently stored.

  55. What steps are used to rehash the described table?

    Save the old slots, double the capacity, reset the size, create a new empty slot array, and reinsert every old key/value pair.

  56. What should insertion do if the key already exists?

    Update the existing node's value rather than adding a new node.

  57. What should insertion do if the key does not exist?

    Add a new key/value node, increment the size, and rehash if the load-factor threshold is exceeded.

  58. How does hash-table lookup determine whether a key exists?

    Hash the key, search the linked list at the resulting index, and check whether a matching node is returned.

  59. How does hash-table retrieval return a value?

    Hash the key, find the matching node in its slot's list, and return its value; return null if the key is absent.

  60. What happens when a key/value pair is successfully deleted?

    The table's size is decremented.

  61. What are examples of built-in hash-table implementations in programming languages?

    dict in Python, HashMap in Java, and unordered_map in C++.

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

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

  63. What is the time complexity of searching a linked list of length lll?

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

  64. What is the time complexity of deleting a node from a linked list of length lll?

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

  65. What is the stated time complexity of the hash function?

    O(1)O(1)O(1) with respect to the number of elements in the hash table.

  66. What is the best-case lookup time in separate chaining?

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

  67. What is the worst-case lookup time in separate chaining?

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

  68. What is the average-case time complexity of insertion in a well-designed hash table?

    O(1)O(1)O(1) amortized.

  69. Why does rehashing not make every insertion take O(n)O(n)O(n) time?

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

  70. What is the average-case time complexity of checking whether a key exists?

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

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

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

  72. What is the average-case time complexity of deleting a key/value pair?

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

  73. What is the worst-case time complexity of the main hash-table operations?

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

  74. What is the space complexity of a hash table storing nnn elements?

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

  75. Why is a hash table often preferred over a simple array for key/value storage?

    It provides average constant-time insertion, lookup, value retrieval, and deletion by key.

  76. What is the key uniqueness rule in a hash table?

    Keys must be unique; the same key should not appear more than once.

  77. Do values in a hash table need to be unique?

    No. Different keys may have the same value.

  78. Why must a hash-table key be immutable?

    Changing the key could change its hash, causing the table to search for it in a different slot from the one used during insertion.

  79. What is an example of how mutating a key causes a problem?

    An array may hash to 565656, then produce a different hash after an element is added, making the original entry difficult to locate.

  80. What are examples of immutable types that can be used as keys?

    Integers, strings, and tuples.

  81. What are examples of mutable types that cannot be used as keys?

    Arrays and sets.

  82. Can a hash-table value be mutable?

    Yes. Values do not need to be immutable because they are not hashed.

  83. What are the two essential properties of a hash-table key?

    It must be unique and immutable.

Card 1 of 83
Card 1 of 83

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent