Amazon Interview Question
Software Engineer / Developershave a sorted stack, so that whenever any new element gets pushed they enter the stack in a sorted manner. Thus min value will be arr[0] or max value will be arr[n-1]
push the items in the stack , while pushing maintain an other variable ( let it be s_max ) , everytime you push , you check this variable with the element being pushed , which ever is greater accordingly update the s_max , similar is the case with finding min as well . you can find max/min just by seeing the top element of the stack( s_max)
All the previous solutions will not work. The reason is that if the top element in stack was the largest and we only used an integer to store the largest value in stack(say maxElement), then pop operation would result in incorrect maxElement. We need to parse the stack to get the max value.
Solution:
Whenever we need to push an integer, we need to create a structure of
{element, Max, Min} and push this structure on stack. currentMin and currentMax is calculated by comparing the previous topmost structure on stack(max, min) and the current integer to be pushed.
If the topmost element was largest and it was poped, then the new topmost element of stack would still have the correct value of Min and Max.
eg: -
Old stack
{element, max, min}
{5, 20, 1}
{20, 20, 1}
{1, 0, 0}
no to be pushed 100.
New Stack
{100, 100, 1}
{5, 20, 1}
{20, 20, 1}
{1, 0, 0}
Now poping this would result in
{5, 20, 1}
{20, 20, 1}
{1, 0, 0}
which still maintins the largest element which is 20.
Let the enhanced stack be called “E”. We can implement “E” using two stacks “S” and “M”.
• Stack “S” keeps track of the normal pushes and pops.
• Stack “M” is used to keep track of the maximum.
To push an item onto Stack “E”, just push the item onto “S”, and also push it onto “M” if it
greater than or equal to the top item of “M”.
To pop on item off Stack “E”, pop the item off “S”, and also pop it off the Stack “M” if it is
equal to the top item in “M”.
To find the maximum item in the Stack “E”, return the top item of “M”.
Note that all of the these operations have running item of O(1).
What if the largest element repeats twice. While pushing into the 2nd stack , it would be pushed once. But, when the maximum element is popped out for the first time, the max element from second stack would be removed, even though it is present in the 1st stack
Consider : 1,2,5,3,5
after removing last 5 .. the second stack will have only 2 :(
To push an item onto Stack “E”, just push the item onto “S”, and also push it onto “M” if it is "greater than or equal" to the top item of “M”.
So in your example (1,2,5,3,5) M will have 5 twice in it. So even after you remove 5 from the top there still would be another 5 left in the M stack.
Solution is correct.
Use two stacks.
The first stack will be used to store the values as they're pushed into the stack. The second stack will be used to save Min values as they arrive. So for the following sequence of numbers: 1, 2, -3, 4
The first stack will be: 1,2,-3,4 (where 4 is at the top of the stack)
The seconds stack will be: 1, -3.
Now, when you pop a value from the first stack, just compare it to the top of the second stack. If they're equal, pop from the second stack as well.
Yosi
Solution :
push(x)
{
if(stack.isEmpty())
{
stack.push(x);
stack.push(x);
}
else
{
if(x > stack.top)
{
min=stack.top;
stack.push(x);
stack.push(min);
}
else
{
stack.push(x);
stack.push(x);
}
}
}
int pop()
{
stack.pop();
return (stack.pop());
}
int min()
{
return stack.top;
}
----------------------------------------------------------
Better solution :
void push(int elem) {
if (stack.empty()) {
stack.push(elem);
stack.push(elem);
} else {
int min = stack.pop();
stack.push(elem - min);
stack.push(elem < min ? elem: min);
}
}
int pop() {
int min = stack.pop(), elem = stack.pop();
if (elem < 0) {
stack.push(min - elem);
return min;
} else {
if (!stack.empty()) stack.push(min);
return elem + min;
}
}
not possible.
- Anonymous September 08, 2009