Computer Sciences > Gate 2016 Set-1 > Queue
A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT (n refers to the number of items in the queue)?
A
Both operations can be performed in O(1) time
B
At most one operation can be performed in O(1) time but the worst case time for the other operation will be Ω(n)
C
The worst case time complexity for both operations will be Ω(n)
D
Worst case time complexity for both operations will be Ω(logn)

Correct : Q

Given:
A queue implemented using an array, with operations ENQUEUE and DEQUEUE performed efficiently.
n is the number of elements in the queue.
To find:
The correct statement regarding the time complexity of ENQUEUE and DEQUEUE operations.
Solution:
Let me think about how a queue is implemented using an array efficiently.
If I use a simple linear array without wrapping around, DEQUEUE might require shifting elements, which takes O(n) time.
Wait, but if we use a circular array implementation with two pointers, front and rear, we can avoid shifting.
Let's see what happens during ENQUEUE in a circular array.
We simply advance the rear pointer using modulo arithmetic:
rear = (rear + 1) % capacity
Then we insert the element at rear, which takes O(1) time.
Now let's check DEQUEUE in a circular array.
We just read the element at front and advance the front pointer:
front = (front + 1) % capacity
This also takes O(1) time.
So both operations can be done in O(1) time using a circular array.
Therefore, option A is clearly the correct statement.
Answer: (A) Both operations can be performed in O(1) time

Similar Questions

Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Sim...
#567 Fill in the Blanks
A palindrome is a word that reads the same forwards and backwards. In a game of words, a player has the following two plates painted with letters. From...
#1 MCQ
Which number does not belong in the series below? 2, 5, 10, 17, 26, 37, 50, 64
#4 MCQ

Related Topics

queue array implementation ENQUEUE DEQUEUE complexity O(1) time complexity GATE Computer Sciences 2016 GATE CS 2016 Set-1 Q27 data structures queue efficient queue operations worst case time complexity

Unique Visitor Count

Total Unique Visitors

Loading......