ProgrammingHardGATE CS · Product-company placement
For a Python list `a` with n elements, which operation is O(n) in the worst case (not amortised O(1))?
- A. a.append(x)
- B. a.insert(0, x)
- C. a.pop()
- D. a[-1] = x
Correct answer: B. a.insert(0, x)
A CPython list is a dynamic array: append and pop from the end are amortised O(1) and index assignment is O(1), but inserting at the front must shift all n existing elements.
More Python Programming questions
- How many of these statements about CPython are correct? 1. A tuple is hashable only if all its elements are hashable. 2. x in someset is…
- In CPython, two threads each run a pure-Python CPU-bound loop (no I/O, no C extensions releasing the lock) on a 4-core machine. Compared…
- Which statement about iterables and iterators is correct?
- Assertion (A): After t = (1, 2, [3]), the statement t[2].append(4) succeeds and changes what print(t) shows. Reason (R): A tuple's…
- Which of these expressions raises an exception in Python 3?
- Which statement about the C3 method resolution order is correct for class D(B, C) where both B and C inherit from A?