Amazon Interview Question for Software Engineer / Developers






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

Are we allowed to have an extra stack data structure ?
If so the algorithm is:

Say S1 and S2 are the 2 stacks, then
enqueue (data) : push data into S1
dequeue:
pop each element in S1 and push it to S2.
Return/remove the top of S2 as the dequeue result.
Pop and push back all elements in S2 to S1.

Basically you are retrieving the last element inserted into the stack S1 by storing all the other elements temporarily into a temp stack.

- CS May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi CS
It is not necessary to "Pop and push back all elements in S2 to S1" in your solution. The next time Pop is called, the topmost item in S2 can be returned. Push will just add the item to S1.
Once S2 is empty and Pop is called, all elements from S1 can be moved to S2 and the topmost item on S2 returned.

- Metta May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, you are correct, thats the efficient way

- CS June 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To come up with a more efficient solution, the question you have to ask is whether or not you can use additional data structures. If you can use a simple array to keep track of the elements pushed to and popped from the stack you can easily determine which element you need to return during queue and enqueue.

- anon May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stack>

std::string deQ(std::stack<std::string>& s)
{
std::string tmp;
std::string val;
if(s.size() > 0)
{
tmp = s.top();
s.pop();
}
if(s.size() > 0)
{
val = deQ(s);
s.push(tmp);
}
else
return tmp;
return val;
}


void main()
{
std::stack<std::string> s;
s.push("one");
s.push("two");
s.push("three");
s.push("four");
std::cout << "The value is : " << deQ(s) << std::endl;
}

- anand.gallant June 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

idea is correct

- bbs59 June 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually this use two stacks, one is the function call stack. Right?

- roger September 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hw can there be 2 return statement ??

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

Don't let the indentation fool you.

It is possible like this, i suppose:

int TwoReturn(int y)
{
  if (y > 0)
  {
    y = 0;
  }
  else
     return -1
  return y;
}

- LOLer July 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* QUEUE USING STACK */

#include<stack>
#include<iostream>

using namespace std;

stack<int> S1;
stack<int> S2;

void enQ(int k)
{
     S1.push(k);
     
     }
     
int deQ()
{
    int t,retv;
    
    if(S2.empty())
    {
     
     while(!S1.empty())
     {
       t=S1.top();
       S1.pop();
       S2.push(t);
       }
       
        retv=S2.top();
        S2.pop();
        return retv;
        }
      
    else
    {
        retv=S2.top();
        S2.pop();
        return retv;
       
        } 
        
        }

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

I am not able to understand one thing in the above solution.
Suppose I have for element in queue, so the pic is like below
S1 S2
4 1
3 2
2 3
1 4
I did 4 deQ(). Then i did another deQ(). what will it return.. it should show that Q is empty.. right. But how it is maintain in the above code??????????????

- San August 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sand: your basic understanding of stack and queue is incomplete, have a look here :
http://en.wikipedia.org/wiki/stack_data_structure
http://en.wikipedia.org/wiki/queue_data_structure

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

Can you please suggest if my algo is right?

public int queFStack(Stack s)
   {
         int value;

         if(s.Count() == 1)
           {
             value = s.pop();
             return(value);
           }
         else
           {
             int m = s.pop();
             value = queFStack(s);
             s.push(m);
             return(value);
           }
   }

The main logic of this code is using internal stack.

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

wow..this algo for dequeue looks just perfect..i.e ofcourse using an internal stack

- daddoo December 28, 2009 | 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