Skip to content
Official PYQ · 2003

GATE CS 2003 Q64 — Arrays, Stacks and Queues

Explain with AI

Let S be a stack of size n≥1n \geq1. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform nn pop operations. Assume that Push and Pop operations take XX seconds each, and YY seconds elapse between the end of one such stack operation and the start of the next operation. For m≥1m \geq1, define the stack-life of mm as the time elapsed from the end of Push(m)Push(m) to the start of the pop operation that removes mm from S. The average stack-life of an element of this stack is

Choose one option
View worked solution

Answer: C

To find the average stack-life, we analyze the timeline of the 2n2n operations: Push(1),…,Push(n)Push(1), \dots, Push(n) followed by Pop(n),…,Pop(1)Pop(n), \dots, Pop(1).

Each operation takes XX seconds, and YY seconds elapse between consecutive operations. Thus, the ii-th operation in the sequence starts at t=(i−1)(X+Y)t = (i-1)(X+Y) and ends at t=iX+(i−1)Yt = iX + (i-1)Y.

For any element m∈{1,2,…,n}m \in \{1, 2, \dots, n\}:

  • It is pushed by the mm-th operation, which ends at:
    tpush_end(m)=mX+(m−1)Yt_{\text{push\_end}}(m) = mX + (m-1)Y
  • It is popped by the (2n−m+1)(2n - m + 1)-th operation, which starts at:
    tpop_start(m)=(2n−m)(X+Y)t_{\text{pop\_start}}(m) = (2n - m)(X+Y)

The stack-life L(m)L(m) of element mm is the duration between these two events:

L(m)=tpop_start(m)−tpush_end(m)=2(n−m)X+(2(n−m)+1)YL(m) = t_{\text{pop\_start}}(m) - t_{\text{push\_end}}(m) = 2(n-m)X + (2(n-m)+1)Y

Letting k=n−mk = n-m, as mm ranges from 11 to nn, kk ranges from 00 to n−1n-1. The average stack-life is:

Average=1n∑k=0n−1[2kX+(2k+1)Y]\text{Average} = \frac{1}{n} \sum_{k=0}^{n-1} \left[ 2kX + (2k+1)Y \right]

Using the identity ∑k=0n−1k=n(n−1)2\sum_{k=0}^{n-1} k = \frac{n(n-1)}{2}:

Average=1n[n(n−1)X+n2Y]=(n−1)X+nY=n(X+Y)−X\text{Average} = \frac{1}{n} \left[ n(n-1)X + n^2Y \right] = (n-1)X + nY = n(X+Y) - X

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

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