Question
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 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 denotes that element was assigned to .
Official answer and solution
- Official answer
- A and B
- From
- Official GATE 2026 answer key
Solution
Note that elements in are output in decreasing order (LIFO) followed by elements in in increasing order (FIFO).
Since the arrival order is :
- All elements assigned to stack are popped in strictly decreasing order.
- All elements assigned to queue are dequeued in strictly increasing order.
In the output 4 3 1 2 5, the split point between and can occur either:
- After
4 3 1: gets (popped as4 3 1) and gets (dequeued as2 5). This corresponds to (Option A). - After
4 3: gets (popped as4 3) and gets (dequeued as1 2 5). This corresponds to (Option B).
Element 1 can either be at the bottom of stack or at the front of queue , so both (A) and (B) produce 4 3 1 2 5.
Options (A), (B)
Overlooking that element 1 sits at the boundary between the decreasing prefix 4 3 1 and the increasing suffix 1 2 5.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2026 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 50 of 65
- 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.