GATE CS 2023 | Question 59

Programming & Data Structures › Stacks & Queues · NAT · 2 marks · Answer: 8

  1. Question 59Programming & Data Structuresnat2 marks
    Sequence . Push into stack S; enqueue into queue Q; then: pop S, dequeue Q, pop S, dequeue Q; dequeue Q and push into S (repeat 3 times); pop S; pop S. The top element of S afterwards is __________

    Answer: 8

    Solution

    Answer: 8 After the pushes S (top->bottom) = 2,9,8,7,5,1 and Q (front->back)=1,5,7,8,9,2.
    pop S -> 2        (S: 9 8 7 5 1)
    dequeue Q -> 1    (Q: 5 7 8 9 2)
    pop S -> 9        (S: 8 7 5 1)
    dequeue Q -> 5    (Q: 7 8 9 2)
    dequeue+push x4: move 7,8,9,2 from Q onto S
      -> S: 2 9 8 7 8 7 5 1   (pushes 7, then 8, then 9, then 2 on top)
    pop S -> 2        (S: 9 8 7 8 7 5 1)
    pop S -> 9        (S: 8 7 8 7 5 1)
    Top of S is 8.

More GATE PYQ on Stacks & Queues

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2023 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →