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)?
GATE CS - 2016,
Answer: A
Both operations can be performed in \(O(1)\) time.
Implement the queue as a circular array with two indices, front and rear. ENQUEUE writes at rear and advances it modulo the array size; DEQUEUE reads at front and advances it modulo the size. Neither operation shifts any elements, so each takes constant time.
The \(\Omega(n)\) options describe a naive array that shifts all elements forward on every DEQUEUE, which the question rules out by saying the operations are performed efficiently. \(\Omega(\log n)\) belongs to heap-based priority queues, not to a simple queue.
Please log in or register to report this question.
