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 1–5 of 14
Consider a stack and a queue . Both of them are initially empty and have the capacity to store ten elements each. The elements , and arrive one by one, in that order. When an element arrives, it is assigned either to (pushed on ) or to (enqueued to ). Once all the five elements are stored, the output is generated in two steps. First, stack S is emptied by popping all elements. Then queue is emptied by dequeueing all elements. The output obtained by following this process is .
Given the output, the objective is to predict whether an element was assigned to or .
Which of the following options is/are possible valid assignment(s) of the elements?
Note: In the options, the notation denotes that element was assigned to and denotes that element was assigned to .
Given the output, the objective is to predict whether an element was assigned to or .
Which of the following options is/are possible valid assignment(s) of the elements?
Note: In the options, the notation denotes that element was assigned to and denotes that element was assigned to .
One or more options are correct.
Answer: (A), (B)
The output has two parts. The stack elements come out first, in reverse of the order they were pushed. Then the queue elements come out in the same order they were enqueued. The target output is .
For each option, split the elements into an list and a list. For the stack list, take the arrival order and then reverse it for popping. For the queue list, keep the arrival order.
- 1S,2Q,3S,4S,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — matches!
- 1Q,2Q,3S,4S,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — matches!
- 1Q,2Q,3Q,4S,5S: Stack gets pushed in the order → popped as . Queue gives: . Combined: — does not match.
- 1S,2S,3S,4Q,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — does not match.
Answer: "1S,2Q,3S,4S,5Q" and "1Q,2Q,3S,4S,5Q" are both valid assignments.
For each option, split the elements into an list and a list. For the stack list, take the arrival order and then reverse it for popping. For the queue list, keep the arrival order.
- 1S,2Q,3S,4S,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — matches!
- 1Q,2Q,3S,4S,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — matches!
- 1Q,2Q,3Q,4S,5S: Stack gets pushed in the order → popped as . Queue gives: . Combined: — does not match.
- 1S,2S,3S,4Q,5Q: Stack gets pushed in the order → popped as . Queue gives: . Combined: — does not match.
Answer: "1S,2Q,3S,4S,5Q" and "1Q,2Q,3S,4S,5Q" are both valid assignments.
Consider a sequence of elements . The following operations are performed on a stack and a queue , both of which are initially empty.
I: push the elements of a from to in that order into .
II: enqueue the elements of a from to in that order into .
III: pop an element from .
IV: dequeue an element from .
V: pop an element from .
VI: dequeue an element from .
VII: dequeue an element from and push the same element into .
VIII: Repeat operation VII three times.
IX: pop an element from .
X: pop an element from .
The top element of after executing the above operations is ___.
I: push the elements of a from to in that order into .
II: enqueue the elements of a from to in that order into .
III: pop an element from .
IV: dequeue an element from .
V: pop an element from .
VI: dequeue an element from .
VII: dequeue an element from and push the same element into .
VIII: Repeat operation VII three times.
IX: pop an element from .
X: pop an element from .
The top element of after executing the above operations is ___.
Answer: 8
After steps I and II: S = [1, 5, 7, 8, 9, 2] (top = 2), Q = [1, 5, 7, 8, 9, 2] (front = 1).
- III: pop S takes out 2
- IV: dequeue Q takes out 1
- V: pop S takes out 9 S = [1, 5, 7, 8]
- VI: dequeue Q takes out 5 Q = [7, 8, 9, 2]
- VII + VIII (4 times): 7, 8, 9, 2 go from Q into S S = [1, 5, 7, 8, 7, 8, 9, 2]
- IX, X: pop 2 and 9 top = 8
Answer: 8
- III: pop S takes out 2
- IV: dequeue Q takes out 1
- V: pop S takes out 9 S = [1, 5, 7, 8]
- VI: dequeue Q takes out 5 Q = [7, 8, 9, 2]
- VII + VIII (4 times): 7, 8, 9, 2 go from Q into S S = [1, 5, 7, 8, 7, 8, 9, 2]
- IX, X: pop 2 and 9 top = 8
Answer: 8
Consider the queues Q1 containing four elements and Q2 containing none (shown as the Initial State in the figure). The only operations allowed on these two queues are and . The minimum number of operations on Q1 required to place the elements of Q1 in Q2 in reverse order (shown as the Final State in the figure) without using any additional storage is
Answer: 0
Do the whole reversal with Q2 only. Each time a new element goes into Q2, move the older elements behind it by dequeuing from Q2 and enqueuing back into Q2.
- Move 1: element 1 Q2 gives [1]
- Move 2: element 2 Q2 gives [1, 2]. Rotate once [2, 1]
- Move 3: element 3 Q2. Rotate twice [3, 2, 1]
- Move 4: element 4 Q2. Rotate 3 times [4, 3, 2, 1]
Q1 is only dequeued. It is never enqueued.
Answer: 0
- Move 1: element 1 Q2 gives [1]
- Move 2: element 2 Q2 gives [1, 2]. Rotate once [2, 1]
- Move 3: element 3 Q2. Rotate twice [3, 2, 1]
- Move 4: element 4 Q2. Rotate 3 times [4, 3, 2, 1]
Q1 is only dequeued. It is never enqueued.
Answer: 0
Consider the following sequence of operations on an empty stack.
push(54); push(52); pop(); push(55); push(62); s=pop();
Consider the following sequence of operations on an empty queue.
enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q=dequeue();
The value of s+q is ___________.
push(54); push(52); pop(); push(55); push(62); s=pop();
Consider the following sequence of operations on an empty queue.
enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q=dequeue();
The value of s+q is ___________.
Answer: 86
Stack: push 54, push 52, then pop (this removes 52). Next push 55 and push 62. The last pop gives s = 62.
Queue: enqueue 21 and 24, then dequeue (this removes 21). Next enqueue 28 and 32. The last dequeue gives q = 24.
So s + q = 86
Answer: 86
Queue: enqueue 21 and 24, then dequeue (this removes 21). Next enqueue 28 and 32. The last dequeue gives q = 24.
So s + q = 86
Answer: 86
A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let enqueue be implemented by inserting a new node at the head, and dequeue be implemented by deletion of a node from the tail.
Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?
Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?
Answer: (B)
Let us check each operation:
- Enqueue at the head: make a new node and link it to the old head .
- Dequeue at the tail: to delete the tail, we need the node just before it. A singly linked list has no back link, so we must travel from the head .
Answer:
- Enqueue at the head: make a new node and link it to the old head .
- Dequeue at the tail: to delete the tail, we need the node just before it. A singly linked list has no back link, so we must travel from the head .
Answer:
Keep practising