Question

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
end

The maximum possible number of iterations of the while loop in the algorithm is _____.

Official answer and solution
Official answer
256
From
Official GATE 2016 answer key

Solution

Model the algorithm as an insertion process that maintains elements sorted on the stack, and identify the worst-case initial order of the queue.

The algorithm processes elements using a queue QQ and stack SS:

  • If SS is empty or Top(S)≤Head(Q)\text{Top}(S) \le \text{Head}(Q), the front element of QQ is dequeued and pushed onto SS.
  • If Top(S)>Head(Q)\text{Top}(S) > \text{Head}(Q), the top element of SS is popped and appended back to the tail of QQ.

This mechanism simulates sorting: elements on stack SS are kept in non-decreasing order from bottom to top. When a new element from QQ is smaller than Top(S)\text{Top}(S), all elements on SS that are strictly greater than that element must be popped back into QQ before the smaller element can be placed on SS.

The worst case occurs when the queue initially contains elements in strictly decreasing order: x1>x2>⋯>xnx_1 > x_2 > \dots > x_n, where n=16n = 16.

  • For the first element x1x_1: SS is empty, so x1x_1 is dequeued and pushed to SS (11 iteration). S=[x1]S = [x_1].
  • For the second element x2x_2 (x2<x1x_2 < x_1):
    1. Top(S)=x1>Head(Q)=x2  ⟹  x1\text{Top}(S) = x_1 > \text{Head}(Q) = x_2 \implies x_1 is popped from SS and enqueued to QQ (11 iteration).
    2. SS is now empty   ⟹  x2\implies x_2 is dequeued from QQ and pushed to SS (11 iteration).
    3. Now Head(Q)=x1≥Top(S)=x2  ⟹  x1\text{Head}(Q) = x_1 \ge \text{Top}(S) = x_2 \implies x_1 is dequeued and pushed to SS (11 iteration).
      Total iterations to insert x2x_2 when 1 element was on the stack: 1+1+1=31 + 1 + 1 = 3 iterations. Cumulative iterations for 2 elements: 1+3=4=221 + 3 = 4 = 2^2.

In general, when kk elements are already sorted on SS and the next element at the front of QQ is smaller than all of them:

  • It takes kk iterations to pop all kk elements from SS into QQ.
  • It takes 11 iteration to dequeue the new element and push it onto the empty stack SS.
  • It takes kk iterations to dequeue each of the kk elements back from QQ and push them onto SS.
    Thus, placing the (k+1)(k+1)-th element takes k+1+k=2k+1k + 1 + k = 2k + 1 iterations.

Summing over all nn elements from k=0k = 0 to n−1n - 1:

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

For n=16n = 16 elements:

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

256

Common trap

Miscounting the loop iterations by considering only the push operations (1616) or neglecting that popped elements must be re-dequeued back into the stack.

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

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 1
Question
51 of 65, printed as CS Q.41
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.