GATE CS 2026 · Set-2 · Arrays & Matrix Representation

MSQ · 2 marks · Problem PROG-DSTR-0077

Consider a stack \(S\) and a queue \(Q\). 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 \(S\) (pushed on \(S\) ) or to \(Q\) (enqueued to \(Q\) ). 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 \(Q\) is emptied by dequeueing all elements. The output obtained by following this process is \(\text{4 3 1 2 5}\). Given the output, the objective is to predict whether an element was assigned to \(S\) or \(Q\). Which of the following options is/are possible valid assignment(s) of the elements? Note: In the options, the notation \(x S\) denotes that element \(x\) was assigned to \(S\) and \(y Q\) denotes that element \(y\) was assigned to \(Q\).
  1. A
    \(1 S, 2 Q, 3 S, 4 S, 5 Q\)
  2. B
    \(1 Q, 2 Q, 3 S, 4 S, 5 Q\)
  3. C
    \(1 Q, 2 Q, 3 Q, 4 S, 5 S\)
  4. D
    \(1 S, 2 S, 3 S, 4 Q, 5 Q\)

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solutionOfficial GATE Answer Key: A, B.See full step-by-step discussion on GateOverflow: https://gateoverflow.in/523106/gate-cse-2026-set-2-question-40

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com