Queue · Data Structures
14 questions · asked in 13 of 27 years (2000–2026)
MCQ 9 · MSQ 1 · NAT 41-mark 7 · 2-mark 7
2000Questions per year2026
Questions 6–10 of 14
A circular queue has been implemented using a single linked list where each node consists of a value and a single pointer pointing to the next node. We maintain exactly two external pointers FRONT and REAR pointing to the front node and the rear node of the queue, respectively. Which of the following statements is/are CORRECT for such a circular queue, so that insertion and deletion operation can be performed in O (1) time ?
I. Next pointer of front node points to the rear node.
II. Next pointer of rear node points to the front node.
I. Next pointer of front node points to the rear node.
II. Next pointer of rear node points to the front node.
Answer: (B)
New nodes go in after REAR, and nodes are removed from FRONT. To do both in time, the next pointer of the rear node has to point to the front node. This is what makes the list circular. The next pointer of the front node points to the second node, not to the rear node.
Answer: II only
Answer: II only
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)?
Answer: (A)
Take a circular array with a front index and a rear index. ENQUEUE puts the item at rear and then moves rear forward. DEQUEUE takes the item from front and then moves front forward. Both indices move modulo the array size. Nothing has to be shifted, so each operation takes time.
Answer: Both operations can be performed in O(1) time
Answer: Both operations can be performed in O(1) time
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. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below.
The maximum possible number of iterations of the while loop in the algorithm is_________.
while Q is not Empty do
if S is Empty OR Top(S) ≤ Head(Q) then
x := Dequeue(Q);
Push(S,x);
else
x := Pop(S);
Enqueue(Q,x);
end
endThe maximum possible number of iterations of the while loop in the algorithm is_________.
Answer: 256
Each iteration is a push (Dequeue then Push) or a pop (Pop then Enqueue). The stack always holds its elements in increasing order from bottom to top. Assume the numbers are distinct; equal numbers only reduce the count. Let be the largest number of iterations for n numbers.
Look at the smallest number m. Before m reaches the head of the queue, only the k elements ahead of it are pushed, . A popped element goes to the tail, behind m, so each of these k elements is pushed once and popped at most once. That is at most iterations. When m reaches the head, it pops everything still on the stack (already counted above) and is pushed: one more iteration. So this phase takes at most iterations.
After that, m stays at the bottom of the stack, because no head is smaller than it, and the test always lets the next element through. So the other elements behave like a fresh run. This gives with , so .
A decreasing queue reaches this bound. The head n is pushed and then popped by n-1, which is pushed and popped by n-2, and so on, and m is pushed last. That is exactly iterations and leaves the other n-1 elements in decreasing order. Check: n = 2 with queue [2, 1] takes 4 iterations, and n = 3 with [3, 2, 1] takes 5 + 4 = 9.
For : .
Answer: 256
Look at the smallest number m. Before m reaches the head of the queue, only the k elements ahead of it are pushed, . A popped element goes to the tail, behind m, so each of these k elements is pushed once and popped at most once. That is at most iterations. When m reaches the head, it pops everything still on the stack (already counted above) and is pushed: one more iteration. So this phase takes at most iterations.
After that, m stays at the bottom of the stack, because no head is smaller than it, and the test always lets the next element through. So the other elements behave like a fresh run. This gives with , so .
A decreasing queue reaches this bound. The head n is pushed and then popped by n-1, which is pushed and popped by n-2, and so on, and m is pushed last. That is exactly iterations and leaves the other n-1 elements in decreasing order. Check: n = 2 with queue [2, 1] takes 4 iterations, and n = 3 with [3, 2, 1] takes 5 + 4 = 9.
For : .
Answer: 256
Suppose a stack implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Which one of the following statements is TRUE with respect to this modified stack?
Answer: (C)
Put the front of the queue at the top of the stack.
- ENQUEUE(x): do REVERSE, then PUSH x, then REVERSE again. This is 3 instructions.
- DEQUEUE: do POP. This is 1 instruction.
Answer: ENQUEUE takes three instructions and DEQUEUE takes one
- ENQUEUE(x): do REVERSE, then PUSH x, then REVERSE again. This is 3 instructions.
- DEQUEUE: do POP. This is 1 instruction.
Answer: ENQUEUE takes three instructions and DEQUEUE takes one
Consider the following operation along with Enqueue and Dequeue operations on queues, where k is a global parameter.
MultiDequeue(Q){
m = k
while (Q is not empty) and (m > 0) {
Dequeue(Q)
m = m - 1
}
} What is the worst case time complexity of a sequence of n queue operations on an initially empty queue?Answer: (A)
Every element is enqueued one time and can be dequeued at most one time. So over n operations, the number of dequeues (the ones inside MultiDequeue too) can never be more than the number of enqueues.
Total work (amortised) =
Answer:
Total work (amortised) =
Answer:
Keep practising