GateAI
Linear Structures, Trees and Heaps

GATE CSE 2016 Set 1 · Q41

NAT2 marksHard

Time

Let QQ denote a queue containing sixteen numbers and SS be an empty stack. Head(Q)\text{Head}(Q) returns the element at the head of the queue QQ without removing it from QQ. Similarly Top(S)\text{Top}(S) returns the element at the top of SS without removing it from SS. Consider the algorithm given below.while Q is not Empty do if S is Empty OR Top(S) <= Head(Q) then x := Dequeue(Q); Push(S, x); else x := Pop(S); Enqueue(Q, x); end endThe maximum possible number of iterations of the while loop in the algorithm is _______.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 256

Linear Structures, Trees and Heaps

Let nn be the number of elements initially in queue QQ (here n=16n = 16).

The algorithm sorts the elements into stack SS in non-decreasing order from bottom to top (or strictly descending when popped).

Let I(n)I(n) denote the maximum number of iterations of the while loop for nn elements.

Consider the worst-case scenario, which occurs when the queue elements are in strictly decreasing order (e.g., n,n−1,…,2,1n, n-1, \dots, 2, 1):

  1. For the first element, the stack is empty →1\rightarrow 1 Push operation (11 iteration).

  2. For the kk-th element (k=2,3,…,nk = 2, 3, \dots, n), all k−1k-1 elements currently in the stack are strictly greater than the new element. Thus, they must all be popped and enqueued back to QQ (k−1k-1 Pop iterations), then the new element is pushed (11 Push iteration), and finally all k−1k-1 popped elements will later be pushed back into the stack (k−1k-1 Push iterations).

Thus, processing the kk-th element adds (k−1)+1+(k−1)=2k−1(k-1) + 1 + (k-1) = 2k - 1 iterations.

Summing over all nn elements from k=1k = 1 to nn:

∑k=1n(2k−1)=n2\sum_{k=1}^{n} (2k - 1) = n^2

Given n=16n = 16:

Total maximum iterations=162=256\text{Total maximum iterations} = 16^2 = 256

Therefore, the maximum possible number of iterations is 256.

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 Q41 of the GATE CSE 2016 Set 1 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 2016 Set 1 has 2.

How GATE tests Linear Structures, Trees and Heaps

More from GATE CSE 2016 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2016 Set 1 · Q41 · NAT

Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below. while Q is not Empty do if S is Empty OR Top(S) The maximum possible number of iterations of the while loop in the algorithm 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.