edited by
31,332 views
81 votes
81 votes

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 $n$ queue operations on an initially empty
queue?

  1. $Θ(n)$
  2. $Θ(n + k)$
  3. $Θ(nk)$
  4. $Θ(n^2)$
edited by

9 Answers

3 votes
3 votes
First of all n Queue operation means any order of enqueue, dequeue, or multidequeue. Now the question asks for worst case scenario which is possible when we do (n-1) enqueue and 1 multidequeue operation in this order. and moreover answers are given in Θ notation means tightest upper bound for worst case. now in worst case we can write complexity as O(n + k) but value of k can never going beyond n so we can say tightest upper bound is Θ (n + n) = Θ (n)
0 votes
0 votes
Ans ->A

In best case it would be Θ(n[for some enqueue (n-k) ]+k[some dequeue operations (k) ]) because you would not be deleting all the elements in that case ,and in worst case you have to delete all the elements ,considering the fact that you cant do more then n dequeue operations and hence answer would be Θ (2n)~ Θ(n), to perform this you can do n-1 enqueue operations and then 1 multidequeue operations where k=n-1.
edited by
0 votes
0 votes

If we see the question its mentioned in initially empty queue and in the code the if condition is false so we don’t enter the loop. Thus for “n” queue operations just the for loop condition is checked “n” times. Thus option A is correct

Answer:

Related questions

52 votes
52 votes
9 answers
2
27 votes
27 votes
2 answers
3