Amazon Interview Question for Software Engineer / Developers






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

2 stacks: one for back_(pop/push) operations and the other one is for front_(pop/push) operations.

For example, if current operation is front operation (second stack involved) and the last was in back (i.e. on 1st stack) than just pop items from 1st and push to 2nd stack. And vice versa.

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

one stack
insert - push - O(1)
dequeue - pop - O(n)

- Anonymous November 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you please give more details?

- eric November 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

queue is FIFO.
insert on top of stack - O(1)
to dequeue - pop all elements in stack - O(n), return last
element popped, push all elements back on stack - O(n)

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

where do you keep the elements when you pop them out of the stack?

- kbekal December 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Queue is First in First Out.
Stack is Last in First Out.
In order to mimic the Queue behavior, we will use two stacks.
Pop() all elements from StackA to StackB one by one - O(n)
Push() new element in StackA, so it stays at the bottom- O(1). Pop() all elements from StackB back to StackA, one by one so that the very first element in StackA would stay on the top- O(n).

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

int insertQ(int item)
{
   stack1.push(item);
   return;
}
int deleteQ(void)
{
  if(isempty(stack1)&&isempty(stack2))
      return UnderFlow; 
  if(!isempty(stack2))
      return pop(stack2);
   while(!isempty(stack1))
      stack2.push(stack1.pop());
   return pop(stack2);
}

- chiragjain1989 January 10, 2011 | 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