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
Total Unique Visitors