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)?
- A. x in some_dict
- B. x in some_list
- C. some_list.index(x)
- 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
- Timsort is the algorithm behind Python's sorted(). For an input that is already in ascending order, what is its running time?
- What is printed? python print(10 17 in range(0, 10 18, 2), 10 17 + 1 in range(0, 10 18, 2))
- What is printed? python def add(x, memo={}): memo[x] = len(memo) return memo print(add(5), add(7))
- What is printed? python print(min('10', '9'), min(10, 9), max('10', '9', key=int))
- What is printed? python i = 'x' = [i for i in range(3)] print(i)
- What is printed? python print([y for x in range(5) if (y := x x) > 5])