StreakPeaked· Practice

ExamsGATETechnical

Consider the following operation along with Enqueue and Dequeue operations on queues, where k 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 queue operations on an initially empty queue?

  1. Θ(n)
  2. Θ(n + k)
  3. Θ(nk)
  4. Θ(n²)

Correct answer: Θ(n)

Solution

The worst-case time complexity is Θ(n) because each Dequeue operation takes constant time, and even if MultiDequeue attempts to dequeue up to k elements, it will only process as many elements as are present in the queue, which is n in total.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →