Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

not possible.

- Anonymous September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maintain the min/max value during PUSH operation

- majun8cn September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

have 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]

- anonymous September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then how the hell can you call it a stack?

- LOLer September 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Y)

- Anonymous March 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Y)

- Anonymous March 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Y)

- Anonymous March 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Anonymous September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Avinash September 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Gud Idea

- Raj Lekkala November 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A circular linked list with sorted values.

- Amit September 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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).

- Anonymous September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea

- Anonymous September 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 :(

- Alice October 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- singh.nikhil October 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Someone September 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
  }
}

- G November 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As there are no constraints on space, a possible solution could be to use a heap for determining the min/ max extry ...

- abhimanipal April 09, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More