Design a stack that supports accessing both its top and middle elements without traversing the stored values.
Every supported operation must run in constant time.
MiddleStack
MiddleStack()
void push(int value)
value to the top of the stack.int pop()
int getMiddle()
int getTop()
n elements, positions are counted from the bottom starting at 1.floor(n / 2) + 1 from the bottom.getMiddle or getTop does not modify the stack.-1,000,000,000 ≤ value ≤ 1,000,000,000100,000 method calls will be made.100,000 elements.pop, getMiddle, and getTop will be called only when the stack is not empty.push, pop, getMiddle, and getTop must each run in O(1) time.O(n) space for n stored elements.MiddleStack()
push(value = 8)
push(value = 3)
push(value = 14)
push(value = 6)
The stack from bottom to top is now [8, 3, 14, 6].
getTop() returns 6.
getMiddle() returns 14.
pop() returns 6.
The stack from bottom to top is now [8, 3, 14].
getMiddle() returns 3.
getTop() returns 14.
MiddleStack()
push(value = -7)
push(value = 12)
The stack from bottom to top is now [-7, 12].
getMiddle() returns 12 because it is the middle element closer to the top.
pop() returns 12.
getTop() returns -7.
getMiddle() returns -7.