GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 44

Amortized cost of MultiDequeue

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2013 paper

GATE 2013, Q44 2 marks · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 44, GATE 2013: Besides constant-time Enqueue/Dequeue, MultiDequeue sets m=k and repeatedly dequeues while the queue is nonempty and m>0, decrementing m. Starting with an empty queue, find worst-case total time for n top-level operations: AΘ(n), BΘ(n+k), CΘ(nk), DΘ(n²).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Let E be the number of Enqueue operations among the n top-level operations. Starting from an empty queue, at most E elements can be removed in total, whether by individual Dequeue calls or inside MultiDequeue. Therefore all successful inner-loop iterations together cost O(E)⊆O(n), not O(nk). Each top-level call and each terminating loop check contribute O(1), adding O(n). Conversely, n calls incur Ω(n) total overhead, and n Enqueue calls witness that bound. The worst-case total is Θ(n), option A, assuming constant-time primitive queue operations and constant-time arithmetic on the counter.

Charge every actual deletion to the earlier insertion that supplied that element. Since an element cannot be deleted twice without being inserted again, the total number of successful dequeues is no larger than the number of Enqueue operations, at most n. MultiDequeue also performs one final failing guard check, but there are at most n top-level calls to pay for those checks. This accounting bounds the complete sequence by a constant times n regardless of k. Multiplying the worst cost of one call by n would ignore the initially empty queue and double-count available elements.

Original paper, page 10 · Answer key