Consider an array 'Q' representing a double ended queue, with Front = 0 and Rear = -1 of...
Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Consider an array 'Q' representing a double ended queue, with Front = 0 and Rear = -1 of size 5. At which location 'i' will get added to 'Q'?
Show answer & explanation
In a deque with Front = 0 and Rear = -1, the Rear = -1 indicates the queue is empty (no elements added yet). When the first element is added to the rear of the deque, Rear is incremented: Rear = -1 + 1 = 0. However, the question asks where the next element (after the first) would be added. When a second element is enqueued at the rear, Rear increments again to 1, so the element is added at location 1.
Step-by-step Derivation:
Initial state: Front = 0, Rear = -1 (empty deque, size = 5, indices 0-4)
1st enqueue operation at rear:
Rear = Rear + 1 = -1 + 1 = 0
Element added at index 0
2nd enqueue operation at rear:
Rear = Rear + 1 = 0 + 1 = 1
Element added at index 1
Assuming the question asks where the next element will be added (implying at least one enqueueing has occurred), the answer is index 1.