Question
A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT ( refers to the number of items in the queue)?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2016 answer key
Solution
Analyze the time complexity of queue operations implemented using a circular array.
A standard efficient array-based queue uses a circular buffer with two integer pointers/indices:
front: points to the head element to be dequeued.rear: points to the tail location where the next element will be enqueued.
Operation execution:
ENQUEUE: Inserts an element atarray[rear]and updatesrear = (rear + 1) % capacity. This involves simple array indexing and arithmetic, running in worst-case time.DEQUEUE: Retrievesarray[front]and updatesfront = (front + 1) % capacity. This also takes worst-case time.
Neither operation requires shifting any elements. Thus, both operations are performed in time.
A
Assuming a linear (non-circular) array where DEQUEUE requires shifting all remaining elements to the left taking time.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 1
- Question
- 20 of 65, printed as CS Q.10
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.