ProgrammingModerateAmazon OA · Flipkart coding round

Which of the following Python operations on a built-in container is O(1) on average (independent of the container size)?

  1. A. x in some_dict
  2. B. x in some_list
  3. C. some_list.index(x)
  4. D. some_list.insert(0, x)

Correct answer: A. x in some_dict

dict and set membership hash the key, giving constant average time. A list membership test, index search and insertion at the front each scan or shift elements, costing O(n).

More Python Programming questions

Exams that ask this topic