Amazon Interview Question for Software Engineer / Developers






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

struct stack{

int minvalue;
int array[MAX_SIZE];
int top;

}mystack;

whenever u push an element just update the value of minvalue.Rest all works like normal stack.

Push,Pop,find_minimum in O(1).

- adhi March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

push(3)
push(2)
push(1)

pop()

what's the min?


have two stacks, one the "normal" stack, and the other mirroring it with the current min for the given stack node. when you pop off of the normal stack, you pop the corresponding min. current min for any given stack is the top of the minstack.

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

import java.util.*;

public class Stack {

	private ArrayList<Comparable> _list;
	private ArrayList<Comparable> _subListMin; 
	
	
	public Stack() {
		
		_list = new ArrayList<Comparable>();
		_subListMin = new ArrayList<Comparable>();
	}
	
	public void push(Comparable item) {
		
		_list.add(item);
		
		if(_subListMin.size()==0) {
			_subListMin.add(item);
			return;
		}
		
		Comparable lastMin =  _subListMin.get(_subListMin.size()-1);
		
		if(lastMin.compareTo(item) < 0) {
			_subListMin.add(lastMin);
		}
		else
		{
			_subListMin.add(item);
		}
	}
	
	public Comparable pop() {
	
		_subListMin.remove(_subListMin.size()-1);
		return _list.remove(_list.size()-1);
	}
	
	public Comparable findMinimum() {
		return _subListMin.get(_subListMin.size()-1);
	}
	
	private String listToString(ArrayList<Comparable> list)
	{
		String res = "";
		for(Comparable c:list) {
			res += c.toString() + ",";
		}
		if(res.length()>0) {
			res = res.substring(0,res.length()-1);
		}
		return res;
	}
	
	public void print() {
		
		System.out.println("Stack content = " + listToString(_list));
		System.out.println("Min = " + findMinimum());
	}
	
	public static void main(String[] args) {
		
		Stack s = new Stack();
	
		s.push(2);
		s.push(8);
		s.push(1);
		s.print();
		
		s.pop();
		s.print();
		
	}
	
}

- TheRunner October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great!!

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

Can you explain your code to store minimum value in subListMin?

Comparable lastMin =  _subListMin.get(_subListMin.size()-1);
		
		if(lastMin.compareTo(item) < 0) {
			_subListMin.add(lastMin);
		}
		else
		{
			_subListMin.add(item);
		}

It appears to me that it is not correct. How will subListMin change if I do,
push(1)
push(2)
push(3)
push(4)
push(5)
push(6)
push(7)
push(8)
push(10)
push(9)

- andy October 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please give the Corresponding C or C++ code for this ?

- Adi November 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi

- Anonymous November 26, 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