edited by
31,218 views
89 votes
89 votes

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

  1. $n(X+Y)$
  2. $3Y+2X$
  3. $n(X+Y)-X$
  4. $Y+2X$
edited by

12 Answers

0 votes
0 votes
Let insert elements 1,2,3 into stack and pop it

1x.  ( Y)   2x.  ( Y)   3x. ( Y)    3x. (Y  )    2x.  ( Y )    1x

Here x represent time required pop or push operation....

Y represent time elapsed between two operations...

Life time of 1 = 5y + 4x

Life time of 2 = 3y + 2x

Life time of 3 = y + Ox

Avg = total time / total values

       = (9y + 6x)/3

        = 3y +2x

For instead of 3 values if we do for 4 values

Avg will be= 4y + 3x

So it is nothing but...  n(x+y)-x
0 votes
0 votes

Consider set A [1...m...n] be the elements.

Case 1: 'm' is the terminal element : Y(wait) $\Rightarrow$ Y
Case 2: 'm' is followed by one element only :  Y(wait) + X(push:m+1) + 
Y(wait) + X(pop:m+1) + Y(wait) $\Rightarrow$ 3Y + 2X
Case 3: 'm' is followed by two elements only :
Y(wait) + X(push:m+1) + 
Y(wait) + X(push:m+2) + Y(wait) + X(pop:m+2) + Y(wait) + X(pop:m+1) + Y(wait) $\Rightarrow$ 5Y + 4X

Hence the answer should be N(X+Y)-X.

0 votes
0 votes
Let there is only one element. So only one element is to be pushed and pop.

| 1 |     |     |

let this be the stack so time elapsed between pushed and poped is Y.

so lifetime = Y when n=1

put n=1 in answer see that in option (c) 1(X+Y)-x=y is matching so c is the answer . no other options are matching.
Answer:

Related questions

79 votes
79 votes
10 answers
2
Disha asked Sep 19, 2014
32,163 views
In a min-heap with $n$ elements with the smallest element at the root, the $7^{th}$ smallest element can be found in time$\Theta (n \log n)$$\Theta (n)$$\Theta(\log n)$$\...
81 votes
81 votes
6 answers
4
Kathleen asked Sep 16, 2014
23,652 views
Let $T(n)$ be the number of different binary search trees on $n$ distinct elements.Then $T(n) = \sum_{k=1}^{n} T(k-1)T(x)$, where $x$ is $n-k+1$$n-k$$n-k-1$$n-k-2$