Amazon Interview Question for Software Engineer / Developers






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

see one thing is there
u have to either compromise with the pop operation or the push operation

- superstart July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use two stacks for this.

For push - push into first stack always. Into second stack, push if it is empty or the value is lesser than peek(second stack).

For pop - Pop(first stack). If value that has been popped = peek(second stack), then pop(second stack).

For min - just peek(second stack).

O(1) for push, pop and min.

- vinod July 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

unless you save all values, you are not guaranteed to have the min value when the current min value is popped. The second data structure could be balanced BST.

- hmmm July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are wrong. The method I posted above is correct. It works because the initial DS is also a Stack. Think about it...!!! Or give me a test case where it fails..!!

- vinod. July 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@vinod
what happens for input: 1,2,3,4,5

- Anonymous August 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it fails for 6, 5, 2, 3 ,2 pushed in this order and then popped once, in that case the min will be 5 while it should be 2.

It gets fixed with a small change it your solution, while pushing - change "lesser than" to "lesser than or equal to"

- dumbdude November 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey28868" class="run-this">package StackDemo;

class Stack {
private int top;
private int[]stackArr;
private int minObj;

Stack(int capacity) throws IllegalArgumentException{
if(capacity <= 0){
throw new IllegalArgumentException("Capacity has to be a positive number");
}
top = -1;
stackArr = new int[capacity];

}

void push(int s){
if(stackArr.length == top){
throw new StackException("Stack's storage is overflowing");
}

stackArr[++top]=s;
if(top==0){
minObj=stackArr[0];
}
if(s<minObj){
minObj=s;
}

}

void pop(){
if (top == -1) {

throw new StackException("Stack is empty");
}
if(minObj==stackArr[top]){
int i=top-1;
int secondminObj = stackArr[i];
while(i>0){
if(stackArr[--i]<secondminObj){
secondminObj=stackArr[i];
}
}

minObj=secondminObj;
}
System.out.println(stackArr[top--]);



}

void peek(){
if (top == -1) {

throw new StackException("Stack is empty");
}
System.out.println(stackArr[top]);

}

boolean isEmpty(){

return top==-1;
}

void emptyStack(){

if (top==-1){
throw new StackException("Stack is empty. Cannot clear the stack");
}
while(top!=-1){
System.out.println(stackArr[top--]);
}

}

boolean search(int obj){
if(top==-1){

throw new StackException("Stack is empty");
}
boolean found = false;
for(int i=0;i<=top;i++){
if(stackArr[i]==obj){
found = true;
break;
}
}
return found;
}


void min(){

System.out.println("The minimum value object is:"+minObj);
}



}

</pre><pre title="CodeMonkey28868" input="yes">
</pre>

- Anonymous December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code with two stack used and one with min stack.
{{
public class StackWithMin {
int MAX_ELEMENT = 20;
int[] origStack = new int[MAX_ELEMENT];
int[] minStack = new int[MAX_ELEMENT];
int origStackTopIndex =-1 ;
int minStackTopIndex =-1;

public void push(int element) throws Exception{
if (origStackTopIndex ==MAX_ELEMENT ) throw new Exception("Stack is full. Max element "+MAX_ELEMENT);
origStack[++origStackTopIndex] = element;
if (origStackTopIndex == 0 || element<= minStack[minStackTopIndex] )
minStack[++minStackTopIndex] = element;

}
public int peek(){
return origStack[origStackTopIndex] ;
}
public int pop() throws Exception{
if (origStackTopIndex == -1) throw new Exception ("Stack is already empty");
if (origStack[origStackTopIndex] == minStack[minStackTopIndex] ) --minStackTopIndex;
return origStack[origStackTopIndex--];
}
public int min(){
return minStack[minStackTopIndex];
}

}


}}
git location

{{
@git httr01/algorithms/blob/master/src/main/java/com/algo/amazon/stack
}}

- hemanttanwar May 14, 2017 | 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