Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

import java.util.Stack;

public class SortingStack {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Stack<Integer> stack = new Stack<>();
		stack.push(5);
		stack.push(1);
		stack.push(5);
		stack.push(2);
		stack.push(9);
		stack.push(3);
		stack.push(0);

		System.out.println(stack);
		sort(stack);
		System.out.println(stack);

	}

	private static void sort(Stack<Integer> stack) {
		if(stack.isEmpty())return;

		int pop = stack.pop();
		sort(stack);
		insertInOrder(stack,pop);
	}

	private static void insertInOrder(Stack<Integer> stack, int element) {
		if(stack.isEmpty() || stack.peek() < element){
			stack.push(element);
		}else{
			int pop = stack.pop();
			insertInOrder(stack,element);
			stack.push(pop);
		}
	}

}

- Goutham March 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Stack:
	def __init__(self):
		self.elements = []
		
	def push(self, elem):
		self.elements.append(elem)
		
	def pop(self):
		return self.elements.pop()
		
	def top(self):
		if self.elements:
			return self.elements[-1]
		else:
			print "stack empty"
	
	def insert_indorder(self, elem):
		if not self.elements or self.top() < elem:
			self.push(elem)
		else:
			top_elem = self.pop()
			self.insert_indorder(elem)
			self.push(top_elem)
	
	def sort_stack(self):
		if not self.elements:
			return
		top_elem = self.pop()
		self.sort_stack()
		self.insert_indorder(top_elem)	

	def print_stack(self):
		for i in self.elements:
			print i,

def main():		
	stack = Stack()
	stack.push(3)
	stack.push(8)
	stack.push(6)
	stack.push(1)
	stack.push(10)
	stack.push(9)
	stack.push(0)
	stack.sort_stack()
	stack.print_stack()

main()

- montu March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Both of the above solutions are using `peek`, which is explicitly prohibited by the problem statement.

- Anonymous April 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is not truly O(1) space. The depth of the recursion depends on the elements in the stack.

- lalat.nayak July 07, 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