GateAI
Linear Structures, Trees and Heaps

GATE CSE 2023 · Q59

NAT2 marksEasy

Time

Consider a sequence aa of elements a0=1a_0 = 1, a1=5a_1 = 5, a2=7a_2 = 7, a3=8a_3 = 8, a4=9a_4 = 9, and a5=2a_5 = 2. The following operations are performed on a stack SS and a queue QQ, both of which are initially empty. I: push the elements of aa from a0a_0 to a5a_5 in that order into SS. II: enqueue the elements of aa from a0a_0 to a5a_5 in that order into QQ. III: pop an element from SS. IV: dequeue an element from QQ. V: pop an element from SS. VI: dequeue an element from QQ. VII: dequeue an element from QQ and push the same element into SS. VIII: Repeat operation VII three times. IX: pop an element from SS. X: pop an element from SS. The top element of SS after executing the above operations is ____________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 8

Linear Structures, Trees and Heaps

Let us trace the state of stack SS and queue QQ after each operation step-by-step:

  1. Initially:

    • Stack S=[]S = []

    • Queue Q=[]Q = []

  2. Operation I (Push a0a_0 to a5a_5 into SS in order):

    • S=[1,5,7,8,9,2]S = [1, 5, 7, 8, 9, 2] (where 22 is at the top)

  3. Operation II (Enqueue a0a_0 to a5a_5 into QQ in order):

    • Q=[1,5,7,8,9,2]Q = [1, 5, 7, 8, 9, 2] (where 11 is at the front)

  4. Operation III (Pop from SS):

    • Pop 22

    • S=[1,5,7,8,9]S = [1, 5, 7, 8, 9]

  5. Operation IV (Dequeue from QQ):

    • Dequeue 11

    • Q=[5,7,8,9,2]Q = [5, 7, 8, 9, 2]

  6. Operation V (Pop from SS):

    • Pop 99

    • S=[1,5,7,8]S = [1, 5, 7, 8]

  7. Operation VI (Dequeue from QQ):

    • Dequeue 55

    • Q=[7,8,9,2]Q = [7, 8, 9, 2]

  8. Operation VII (Dequeue from QQ and push to SS):

    • Dequeue 77 from QQ and push 77 into SS

    • Q=[8,9,2]Q = [8, 9, 2]

    • S=[1,5,7,8,7]S = [1, 5, 7, 8, 7]

  9. Operation VIII (Repeat operation VII three times):

    • *1st time:* Dequeue 88, push 88 into SS   ⟹  Q=[9,2]\implies Q = [9, 2], S=[1,5,7,8,7,8]S = [1, 5, 7, 8, 7, 8]

    • *2nd time:* Dequeue 99, push 99 into SS   ⟹  Q=[2]\implies Q = [2], S=[1,5,7,8,7,8,9]S = [1, 5, 7, 8, 7, 8, 9]

    • *3rd time:* Dequeue 22, push 22 into SS   ⟹  Q=[]\implies Q = [], S=[1,5,7,8,7,8,9,2]S = [1, 5, 7, 8, 7, 8, 9, 2]

  10. Operation IX (Pop from SS):

    • Pop 22

    • S=[1,5,7,8,7,8,9]S = [1, 5, 7, 8, 7, 8, 9]

  11. Operation X (Pop from SS):

    • Pop 99

    • S=[1,5,7,8,7,8]S = [1, 5, 7, 8, 7, 8]

After all operations, the top element of stack SS is 8.

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 Q59 of the GATE CSE 2023 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. 2 of them are 2-mark NAT questions like this one, and GATE CSE 2023 has 2.

How GATE tests Linear Structures, Trees and Heaps

More from GATE CSE 2023

Share this solution

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

GateAI solution cardGATE CSE 2023 · Q59 · NAT

Consider a sequence a of elements a0 = 1, a1 = 5, a2 = 7, a3 = 8, a4 = 9, and a5 = 2. The following operations are performed on a stack S and a queue Q, both of which are initially empty. I: push the elements of a from a0 to a5 in that order into S. II: enqueue the elements of a from a0 to a5 in that order into Q. III: pop an element from S. IV: dequeue an element from Q. V: pop an element from S. VI: dequeue an element from Q. VII: dequeue an element from Q and push the same element into S. VIII: Repeat operation VII three times. IX: pop an element from S. X: pop an element from S. The top element of S after executing the above operations is …

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.