Skip to content
Official PYQ · 2021

GATE CS 2021 Set 1 Q21 — Arrays, Stacks and Queues

Explain with AI
Consider the following sequence of operations on an empty stack.push(54);push(52);pop();push(55);push(62);s=pop();\textsf{push}(54);\textsf{push}(52);\textsf{pop}();\textsf{push}(55);\textsf{push}(62);\textsf{s}=\textsf{pop}();Consider the following sequence of operations on an empty queue.enqueue(21);enqueue(24);dequeue();enqueue(28);enqueue(32);q=dequeue();\textsf{enqueue}(21);\textsf{enqueue}(24);\textsf{dequeue}();\textsf{enqueue}(28);\textsf{enqueue}(32);\textsf{q}=\textsf{dequeue}(); The value of s+q\textsf{s+q} is ___________.
View worked solution

Answer: 86 : 86

To find the value of s+qs + q, we trace the operations on the stack and the queue step-by-step.

1. Stack Operations

A stack follows the Last-In, First-Out (LIFO) principle. Let the rightmost element represent the top of the stack.

  • push(54): Stack = [54][54]
  • push(52): Stack = [54,52][54, 52]
  • pop(): Removes 5252. Stack = [54][54]
  • push(55): Stack = [54,55][54, 55]
  • push(62): Stack = [54,55,62][54, 55, 62]
  • s = pop(): Removes the top element. Thus, s=62s = 62. Stack = [54,55][54, 55]

2. Queue Operations

A queue follows the First-In, First-Out (FIFO) principle. Let the leftmost element represent the front of the queue and the rightmost represent the rear.

  • enqueue(21): Queue = [21][21]
  • enqueue(24): Queue = [21,24][21, 24]
  • dequeue(): Removes the front element 2121. Queue = [24][24]
  • enqueue(28): Queue = [24,28][24, 28]
  • enqueue(32): Queue = [24,28,32][24, 28, 32]
  • q = dequeue(): Removes the front element. Thus, q=24q = 24. Queue = [28,32][28, 32]

3. Calculation

Substituting the values of ss and qq:

s+q=62+24=86s + q = 62 + 24 = 86

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions