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?
- A. About O(N^2), about 10^12 operations
- B. About O(N log N) with a comparison-based tree for each insert
- C. About O(N^3) because each insert rehashes everything
- 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
- Which property of the hash function matters most to avoid long chains when keys are multiples of 8 and the table has 8 buckets?
- A table of size 7 with h(k) = k % 7 is filled with 7 keys using linear probing and no deletions. How many more keys can be inserted before…
- Which search code is correct for an open-addressing table of size m with linear probing, where an empty slot ends the search?
- A hash table stores pointers to nodes allocated with malloc. What must happen when the table is destroyed?
- The expected number of probes for an unsuccessful search in linear probing with load factor a = 0.9 is about what?
- For a hash map of N = 10^5 entries with 10^5 lookups, which implementation is correct and fast enough in C?