Let S be a stack of size . Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform pop operations. Assume that Push and Pop operations take seconds each, and seconds elapse between the end of one such stack operation and the start of the next operation. For , define the stack-life of as the time elapsed from the end of to the start of the pop operation that removes from S. The average stack-life of an element of this stack is
View worked solution
Answer: C
To find the average stack-life, we analyze the timeline of the operations: followed by .
Each operation takes seconds, and seconds elapse between consecutive operations. Thus, the -th operation in the sequence starts at and ends at .
For any element :
- It is pushed by the -th operation, which ends at:
- It is popped by the -th operation, which starts at:
The stack-life of element is the duration between these two events:
Letting , as ranges from to , ranges from to . The average stack-life is:
Using the identity :
Free practice · Your answers on this page are not saved.
