ProgrammingHardProduct-company MCQ pattern

A program makes 10^5 membership checks of the form x in visited, where visited holds 10^5 items. Which statement is correct?

  1. A. A list costs O(n) per check, so the worst case is about 10^10 comparisons, while a set costs O(1) on average per check
  2. B. A list is faster because its contiguous memory makes the whole loop O(n) in total
  3. C. A set costs O(n) per check because it must rehash on every lookup
  4. D. Both are O(n log n) in total because Python sorts the container before searching

Correct answer: A. A list costs O(n) per check, so the worst case is about 10^10 comparisons, while a set costs O(1) on average per check

A list membership test scans items one by one, so the cost per check is O(n). A set uses hashing, giving O(1) average per check, which turns the whole loop into roughly O(n + m) instead of O(n * m).

More Python Programming questions

Exams that ask this topic