Data Structures and Algorithms

For a hash table with many elements kept at a load factor of about 0.5, which is the most appropriate reason that open addressing with linear probing tends to be faster than chaining (linking elements with linked lists)?

  1. AAfter a collision, the next position checked is the neighboring element, which is often in the same cache line or the next one, so fewer memory accesses are needed
  2. BOpen addressing has no collisions at all
  3. CChaining must compute the hash function two or more times
Show answer and explanation

Answer: A. After a collision, the next position checked is the neighboring element, which is often in the same cache line or the next one, so fewer memory accesses are needed

On modern CPUs, reading main memory after a cache miss takes far longer than computations such as comparisons. Linear probing checks the neighboring position after a collision, so most probes stay within the same cache line, and prefetching also helps. With chaining, the next element of a list is often somewhere else in memory, so each pointer followed is likely to cause a cache miss. Even with the same big-O complexity, the real speed can differ greatly depending on how memory is accessed. However, linear probing slows down sharply at high load factors as occupied positions cluster, so the load factor is kept fairly low.

  • A. Correct. It only checks neighboring positions, so the data needed is likely to be in the cache already.
  • B. Collisions also occur in open addressing; it handles them by checking the neighboring positions.
  • C. Both compute the hash function once; the difference is in how memory is accessed.
Practice this topic Back to topic

Related calculators

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

More Data Structures and Algorithms questions

  1. Sorting methods with a fixed sequence of compare-exchange steps (sorting networks), such as bitonic sort, are sometimes used for sorting on parallel hardware such as GPUs. Why?
  2. For an array of n elements, what is the time complexity of comparing every pair of distinct elements exactly once (a double loop that compares element i with every element after it, advancing i one step at a time)?
  3. A program with O(n²) time complexity takes 0.5 seconds for 1,000 data items. About how long will it take for 4,000 items?
  4. A linear search compares the elements of an array of 100 distinct values one by one from the front to find a target. If the target is always in the array and is equally likely to be at any position, what is the average number of comparisons?
  5. Compared with an array, which is a correct description of a linked list (a list in which each element holds a pointer to the next element)?