edited by
31,138 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

1 votes
1 votes
Lets solve it for 3 elements and verify the options to find the correct ans.
for 3rd element pushed on to stack Life time is: Y
for 2nd element pushed on to stack Life time is: 2X+2Y+Y
for 1st element pushed on to stack Life time is: 4X+4Y+Y
therefore average life time for 3 elements: (6X+9Y)/3=2X+3Y

option (c) putting n=3 gives 2x+3y therefore (c) is the correct ans.
1 votes
1 votes
All those calculations might take a while, so I tried to look at this from another quicker perspective.

Let's say n=1 and the average stack-life for m elements (in this case only 1) will then be just Y. So definitely, Y>X. We can strike out options a and d totally.

So how do we pick in between b and c? Now let's assume n is infinitely large. There would be no way it would be option b since it's too less and therefore, option c makes more sense.

Also, if we substitute n=1 into option c, we get Y which is correct. I guess this kind of perspective can be taken into consideration if you are not sure of the answer with the technique used earlier. Hope this helps.
Answer:

Related questions

79 votes
79 votes
10 answers
2
Disha asked Sep 19, 2014
32,011 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,568 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$