GateAI
Linear Structures, Trees and Heaps

GATE CSE 2026 Set 2 · Q50

MSQ2 marksMedium

Time

Consider a stack SS and a queue QQ. Both of them are initially empty and have the capacity to store ten elements each. The elements 1,2,3,4,1, 2, 3, 4, and 55 arrive one by one, in that order. When an element arrives, it is assigned either to SS (pushed on SS) or to QQ (enqueued to QQ). Once all the five elements are stored, the output is generated in two steps. First, stack SS is emptied by popping all elements. Then queue QQ is emptied by dequeueing all elements. The output obtained by following this process is 4 3 1 2 54\ 3\ 1\ 2\ 5 . Given the output, the objective is to predict whether an element was assigned to SS or QQ. Which of the following options is/are possible valid assignment(s) of the elements? *Note:* In the options, the notation xSxS denotes that element xx was assigned to SS and yQyQ denotes that element yy was assigned to QQ.

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: A) 1S, 2Q, 3S, 4S, 5Q; B) 1Q, 2Q, 3S, 4S, 5Q

Linear Structures, Trees and Heaps

The elements arrive in the order 1,2,3,4,51, 2, 3, 4, 5.

When all elements have been assigned:

  1. The stack SS is popped completely (LIFO order: reverse of arrival order in SS).

  2. The queue QQ is dequeued completely (FIFO order: same as arrival order in QQ).

The combined output produced is 4,3,1,2,54, 3, 1, 2, 5.

Let us test each option:

  • Option (A): 1S,2Q,3S,4S,5Q1S, 2Q, 3S, 4S, 5Q

    • Elements pushed to SS in arrival order: 1,3,41, 3, 4. Popping SS gives: 4,3,14, 3, 1.

    • Elements enqueued to QQ in arrival order: 2,52, 5. Dequeueing QQ gives: 2,52, 5.

    • Combined output: 4,3,1,2,54, 3, 1, 2, 5. (Valid)

  • Option (B): 1Q,2Q,3S,4S,5Q1Q, 2Q, 3S, 4S, 5Q

    • Elements pushed to SS in arrival order: 3,43, 4. Popping SS gives: 4,34, 3.

    • Elements enqueued to QQ in arrival order: 1,2,51, 2, 5. Dequeueing QQ gives: 1,2,51, 2, 5.

    • Combined output: 4,3,1,2,54, 3, 1, 2, 5. (Valid)

  • Option (C): 1Q,2Q,3Q,4S,5S1Q, 2Q, 3Q, 4S, 5S

    • Elements pushed to SS: 4,54, 5. Popping SS gives: 5,4≠4,3,…5, 4 \neq 4, 3, \dots (Invalid)

  • Option (D): 1S,2S,3S,4Q,5Q1S, 2S, 3S, 4Q, 5Q

    • Elements pushed to SS: 1,2,31, 2, 3. Popping SS gives: 3,2,1≠4,3,…3, 2, 1 \neq 4, 3, \dots (Invalid)

Thus, options (A) and (B) are correct.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Linear Structures, Trees and Heaps previous year question is Q50 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Linear Structures, Trees and Heaps in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Linear Structures, Trees and Heaps PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Linear Structures, Trees and Heaps in GATE CSE

Linear Structures, Trees and Heaps has 14 GATE CSE PYQs across 12 papers from 2012 to 2026. This is the only 2-mark MSQ among them, and it is the only one in GATE CSE 2026 Set 2.

How GATE tests Linear Structures, Trees and Heaps

More from GATE CSE 2026 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 2 · Q50 · MSQ

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 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 xS denotes that element x was assigned to S and yQ denotes that element y was assigned to Q.

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.