ProgrammingHardCodeforces-style · placement coding round

A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?

  1. A. The list version does about 10^12 comparisons and the set version about 10^6 hash lookups
  2. B. Both versions do about 10^6 operations because list membership uses binary search
  3. C. The set version does about 10^12 hash lookups and the list version about 10^6
  4. D. Both versions are O(log n) per query

Correct answer: A. The list version does about 10^12 comparisons and the set version about 10^6 hash lookups

A list membership test scans linearly, which is O(n) per query, so 10^6 queries cost about 10^12 comparisons. A set uses hashing with O(1) average lookup, so the same queries take about 10^6 operations.

More Python Programming questions

Exams that ask this topic