ProgrammingHard

A program counts the distinct keys among 10^6 values by inserting them into a hash set with expected O(1) operations. What is the expected total time?

  1. A. About O(N^2), about 10^12 operations
  2. B. About O(N log N) with a comparison-based tree for each insert
  3. C. About O(N^3) because each insert rehashes everything
  4. D. About O(N) expected time, which fits well within a second in C

Correct answer: D. About O(N) expected time, which fits well within a second in C

Each insert is expected O(1) with a good hash and load factor, so the whole pass is O(N). The quadratic options would be too slow for N = 10^6.

More C Programming questions

Exams that ask this topic