ProgrammingModerateAmazon OA · Flipkart coding round
What is the time complexity of `x in d` where d is a collections.deque of n items?
- A. O(log n), because a deque is kept sorted
- B. O(n log n), because the deque is sorted before the search
- C. O(n), because deque membership scans the items one by one
- D. O(1), because a deque is a hash table
Correct answer: C. O(n), because deque membership scans the items one by one
A deque is a doubly linked block structure with no hash index, so membership scans in O(n). For fast membership use a set or dict.
More Python Programming questions
- What is printed? python x = 10 print(x.bitlength(), (x >> 1).bitlength(), (x << 3) // 16)
- What is printed? python words = ['tea', 'eat', 'tan', 'ate', 'nat'] groups = {} for w in words: groups.setdefault(''.join(sorted(w))…
- What is printed? python print(0.2 + 0.4 + 0.1 == 0.7, 0.1 + (0.2 + 0.4) == 0.7, 0.2 + 0.1 + 0.4 == 0.7)
- What is printed? python print(sorted([(1, 'b'), (1, 'a'), (0, 'z')], reverse=True)[0])
- What is printed? python print(list(map(len, ['', 'ab', 'abc'])), sum(map(len, ['', 'ab', 'abc'])))
- What is printed? python print(10 in range(0, 20, 5), 11 in range(0, 20, 5))