Given a few numbers, how will you push them into a stack such that whenever I pop the top most element from that stack, I get the minimum number from the bunch. Also he wanted me to tell the pop push algorithm such that the order of insertion and popping should be O(1).
template <typename T>
class MinStack {
private:
std::stack<T> stack;
std::stack<T> min;
public:
MinStack() {}
void push(T elem) {
stack.push(elem);
min.push(std::min(elem, min.top());
}
std::pair<T, T> popValAndMin() {
T val = stack.top();
T minVal = min.top();
stack.pop();
min.pop();
return minVal;
}
};