GeeksforGeeks » Interview Questions
worksApp interview question
(2 posts)-
implement a stack which will support three additional operations in addition to push and pop:
1.peekLowestElement()//return the lowest element in the stack without removing it from the stack
2.peekHighestElement()//return the highset element in the stack without removing it from the stack
3.peekMiddleElement()//return the (size/2 +1)th lowest element in the stack without removing it from the stack.faster is better.Assume every method will be called with equal frequency.
-
If we consider array based implementation of stack , then all the three operations can be done in constant time.
we need to store index of maximum and minimum elements in stack and compare the value at each push() ..
Reply
You must log in to post.