ProgrammingHardProduct-company MCQ pattern
Which approach removes duplicates from a list while keeping the first-seen order, in O(n) average time?
- A. list(set(items))
- B. sorted(set(items), key=items.index)
- C. [x for x in items if x not in set(items)]
- D. list(dict.fromkeys(items))
Correct answer: D. list(dict.fromkeys(items))
dict keys are unique and keep insertion order, so fromkeys does one hash insert per item. set() loses the order, and the other two options are O(n squared) or worse.
More Python Programming questions
- Which statement about sorting 10^6 floats with sorted() in CPython is correct?
- Which operation is O(1) amortised in CPython?
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(a, b=0): return a + b f(1, 2) f(1, b=2) f(1)…
- What is printed? python from collections import OrderedDict od = OrderedDict() for k in ['a', 'b', 'c']: od[k] = ord(k) od.movetoend('a')…
- What is printed? python b = [3, 1, 2] a = b.sort() c = sorted(b, reverse=True) print(a, b, c)
- What is printed? python def kadane(a): best = cur = a[0] start = bestl = bestr = 0 for i in range(1, len(a)): if cur < 0: cur = a[i] start…