Question

Consider the following operation along with Enqueue and Dequeue operations on queues, where kk is a global parameter.

MultiDequeue(Q){
    m = k
    while (Q is not empty) and (m > 0) {
        Dequeue(Q)
        m = m - 1
    }
}

What is the worst case time complexity of a sequence of nn queue operations on an initially empty queue?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2013 answer key

Solution

Use aggregate amortized analysis by tracking the lifecycle of each element enqueued in the queue.

Consider an arbitrary sequence of nn operations performed on an initially empty queue QQ.

  1. The permissible operations are Enqueue, Dequeue, and MultiDequeue.
  2. Since each Enqueue operation inserts exactly one element into QQ, at most nn elements can ever be enqueued across all nn operations.
  3. An element can be removed by a Dequeue operation (whether called directly or called inside MultiDequeue) at most once.
  4. Therefore, across the entire sequence of nn operations, the total number of times Dequeue(Q) can be executed is at most nn.
  5. Each Enqueue takes O(1)O(1) time. Outside the while-loop, each MultiDequeue takes O(1)O(1) time to initialize mm. The while-loop iterations in MultiDequeue execute Dequeue(Q), each iteration accounting for the removal of one enqueued element.
  6. Since at most nn removals can occur in total, the cumulative number of while-loop iterations over all operations in the sequence cannot exceed nn.

Thus, the total time for the entire sequence of nn operations is upper-bounded by O(n)O(n). Since each operation takes at least Ω(1)\Omega(1) time, the sequence takes Ω(n)\Omega(n) time. Therefore, the worst-case time complexity is Θ(n)\Theta(n), corresponding to option (A).

Final answer

A

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2013 Computer Science and Information Technology (CS)
Question
44 of 65, printed as CS Q.44
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.