Data Structures & AlgorithmsModerateTCS NQT · hashing

Problem: A hash table uses chaining, and every insertion first scans the key’s chain to make sure the key is not already present. What is the worst-case time of one insertion when all keys collide?

  1. A. O(1) per insertion
  2. B. O(n) per insertion
  3. C. O(log n) per insertion
  4. D. O(n^2) per insertion

Correct answer: B. O(n) per insertion

All keys hash to one bucket, so its chain holds up to n keys, and the duplicate check scans the whole chain before inserting: O(n). Inserting at the head without that check would be O(1), which is why the check is stated.

More Coding Interview Problems questions

Exams that ask this topic