Computer Sciences > GATE 2026 SET-2 > Data Structures
Consider a stack 𝑆 and a queue 𝑄. Both of them are initially empty and have the capacity to store ten elements each. The elements 1,2,3,4, and 5 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 4 3 1 2 5 . 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 y𝑄 denotes that element 𝑦 was assigned to 𝑄.
A
1S, 2Q, 3S, 4S, 5Q
B
1Q, 2Q, 3S, 4S, 5Q
C
1Q, 2Q, 3Q, 4S, 5S
D
1S, 2S, 3S, 4Q, 5Q

Correct : a,b

The output is 4 3 1 2 5. Stack S is emptied first by popping (LIFO), then Queue Q is emptied by dequeueing (FIFO). So whatever comes out of S appears at the front of the output, and whatever comes out of Q follows at the end.
Option A β€” 1S, 2Q, 3S, 4S, 5Q
S gets 1, 3, 4 in that order. Popping gives 4, 3, 1. Q gets 2, 5. Dequeueing gives 2, 5. Full output: 4 3 1 2 5 βœ“
Option B β€” 1Q, 2Q, 3S, 4S, 5Q
S gets 3, 4. Popping gives 4, 3. Q gets 1, 2, 5. Dequeueing gives 1, 2, 5. Full output: 4 3 1 2 5 βœ“
Option C β€” 1Q, 2Q, 3Q, 4S, 5S
S gets 4, 5. Popping gives 5, 4. But output starts with 4 3, not 5 4. βœ—
Option D β€” 1S, 2S, 3S, 4Q, 5Q
S gets 1, 2, 3. Popping gives 3, 2, 1. But output starts with 4 3, not 3 2. βœ—
Correct answer: A and B βœ“

Similar Questions

Which one of the following sequences when stored in an array at locations A[1],...,A[10] forms a max-heap?
#950 MCQ
Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let DLLdel be...
#951 MCQ
An algorithm has to store several keys generated by an adversary in a hash table. The adversary is malicious who tries to maximize the number of collisions. Let...
#957 MCQ

Related Topics

stack queue assignment GATE 2026 GATE CS 2026 Set-2 Q50 stack pop queue dequeue output data structures GATE 2026 stack LIFO queue FIFO GATE valid assignment elements stack queue output 4 3 1 2 5 GATE GATE computer science data structures 2026

Unique Visitor Count

Total Unique Visitors

Loading......