ProgrammingModerateProduct-company MCQ pattern
Which operation is O(1) on average in CPython?
- A. Checking membership with x in a list
- B. Calling list.index(x) on a list of n items
- C. Calling str.count on a string of n characters
- D. Looking up a value by key in a dict
Correct answer: D. Looking up a value by key in a dict
A dict uses hashing to find a key's slot in about constant time on average. The other three each scan the container, which costs O(n).
More Python Programming questions
- A loop runs s += c for 100000 single characters in CPython. Which statement is most accurate?
- Which statement about converting a string to an int in Python 3.13 is correct?
- What relative and absolute tolerances does math.isclose use by default?
- Which key sorts records by score descending and then by name ascending, using one stable sort?
- What is the largest float that is less than 1.0 in Python, as printed by repr?
- What is printed? python print(3 // 2 2 + 3 % 2, -3 // 2 2 + -3 % 2)