Question

Consider the following sequence of operations on an empty stack.

push(54); push(52); pop(); push(55); push(62); s = pop();

Consider the following sequence of operations on an empty queue.

enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();

The value of s+qs + q is __________

Official answer and solution
Official answer
86
From
Official GATE 2021 answer key

Solution

Trace the step-by-step state of the stack and the queue to find the values of s and q.

Trace the stack operations (initially empty):

  1. push(54): stack is [54]
  2. push(52): stack is [54, 52]
  3. pop(): removes 52; stack is [54]
  4. push(55): stack is [54, 55]
  5. push(62): stack is [54, 55, 62]
  6. s = pop(): removes and returns 62. Thus s=62s = 62.

Trace the queue operations (initially empty, FIFO order):

  1. enqueue(21): queue is [21]
  2. enqueue(24): queue is [21, 24]
  3. dequeue(): removes 21; queue is [24]
  4. enqueue(28): queue is [24, 28]
  5. enqueue(32): queue is [24, 28, 32]
  6. q = dequeue(): removes and returns the front element, 24. Thus q=24q = 24.

Computing the sum:

s+q=62+24=86s + q = 62 + 24 = 86
Final answer

86

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2021 Computer Science and Information Technology (CS, Set-1)
Question
31 of 65, printed as CS Q.21
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.