Data Structures & AlgorithmsModerateProduct-company MCQ pattern
In Python, a list q is used as a queue: items are added with q.append(x) and removed with q.pop(0). If n items pass through the queue, which statement is correct?
- A. append is O(n), so the cost is dominated by appends
- B. The total cost is O(n) because both operations are O(1)
- C. pop(0) is O(log n), so the total is O(n log n)
- D. The total cost is O(n²) because pop(0) shifts all remaining items; collections.deque.popleft() would make it O(n)
Correct answer: D. The total cost is O(n²) because pop(0) shifts all remaining items; collections.deque.popleft() would make it O(n)
A Python list is a dynamic array: pop(0) moves every remaining element one place left, which is O(n) each time. deque is a linked block structure with O(1) operations at both ends; append on a list is O(1) amortised.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What does this function print for the list 1 → 2 → 3 → 4 → 5 → 6? python def f(node): if node is None: return if node.next…
- slow and fast both start at the head of 1 → 2 → 3 → 4 → 5 → 6. Loop A: while (fast && fast->next) { slow = slow->next; fast =…
- What is the maximum number of comparisons top-down merge sort can make on 8 elements (any input order)?
- What is the minimum number of comparisons needed, in the worst case, to find BOTH the maximum and the minimum of 100 distinct numbers?
- One Lomuto partition (pivot = last element, i marks the end of the 'smaller' part, swap when a[j] < pivot) is applied to [7, 2, 9, 4, 3…
- Insertion sort (ascending) is applied to [5, 2, 4, 6, 1, 3]. What does the array look like after the first 3 passes (i.e. after inserting…