Microsoft Interview Question
Software Engineer in TestsA little optimization in above (It's NEVER required to move elements from s2 to s1. Also move from s1 to s2 is needed only when s2 is EMPTY)
1. Have two stacks, s1 and s2.
2. If Enqueue, push into s1
3. If Dequeue, pop from s2 (if s2 is not empty). If s2 is empty, move all elements from s1 to s2 (pop from s1 and push in s2). Now pop from s2.
Enqueue Cost: O(1)
Dequeue Cost: O(1) -- Amortized
WOW.. Appreciated. Good logic.
If anybody publish some code then that will really help.
WOW.. Appreciated. Good logic.
If anybody publish some code then that will really help.
public class QeueUsingTwoStacks
{
Stack pushStack = new Stack();
Stack popStack = new Stack();
public void Enqueue(int data)
{
Console.WriteLine("Enqueued:{0}", data);
pushStack.Push(data);
}
public SinglyNode Dequeue()
{
if (popStack.isEmpty())
{
while (!pushStack.isEmpty())
popStack.Push(pushStack.Pop().data);
}
return popStack.Pop();
}
}
#include <iostream>
using namespace std;
const int Max = 10;
class stack
{
public:
int arr[Max],top;
stack();
void push(int item);
int pop();
};
stack::stack()
{
top = -1;
}
void stack::push (int data)
{
if (top == Max-1)
{
cout << "\n the stack is full";
return;
}
top++;
arr[top] = data;
}
int stack::pop()
{
if (top == -1)
{
cout << "\n the stack is empty";
return NULL;
}
int data = arr[top];
top--;
return data;
}
void main()
{
stack *S1,*S2;
S1 = new stack();
S2 = new stack();
void enqueue (stack *S1, int item);
int dequeue(stack *S1, stack *S2);
void displayQueue(stack *S1,stack *S2);
enqueue(S1,1);
enqueue(S1,2);
enqueue(S1,3);
displayQueue(S1,S2);
dequeue(S1,S2);
enqueue(S1,4);
enqueue(S1,5);
displayQueue(S1,S2);
dequeue(S1,S2);
dequeue(S1,S2);
dequeue(S1,S2);
dequeue(S1,S2);
displayQueue(S1,S2);
enqueue(S1,6);
enqueue(S1,7);
enqueue(S1,8);
displayQueue(S1,S2);
dequeue(S1,S2);
displayQueue(S1,S2);
}
void enqueue(stack *S1,int item)
{
S1->push (item);
}
int dequeue(stack *S1, stack *S2)
{
if (S2->top == -1)
{
for (int i=S1->top;i>-1;i--)
{
S2->push(S1->pop());
}
}
int data = S2->pop();
cout << "\n the dequeued element is" << data;
return (data);
}
void displayQueue(stack *S1,stack *S2)
{
if (S2->top ==-1)
{
if (S1->top == -1)
{
cout << "\n the Queue is empty";
return;
}
else
{
for (int i=0;i<=S1->top;i++)
{
cout << "\n the" << i+1<<"th element is"<<S1->arr[i] ;
}
}
}
else
{
for (int j=S2->top;j>-1;j--)
{
cout << "\n the" << S2->top-j+1<<"th element is"<<S2->arr[j] ;
}
for (int k=S1->top;k>-1;k--)
{
cout << "\n the" << S2->top+2+S1->top-k << "th element is" << S1->arr[S1->top-k];
}
}
}
Using only one stack
struct node {
int num;
struct node *link;
};
typedef struct node node1;
struct node *getnode()
{
return (struct node*)malloc(sizeof(struct node));
}
void Display(struct node *p)
{
while(p)
{
printf("%d \n",p->num);
p=p->link;
}
}
void QInsert(node1 **top,int num)
{
node1 *temp = getnode();
temp->num=num;
temp->link=*top;
*top=temp;
}
int QDelete(node1 **top)
{
node1 *f=*top;
int num;
if(*top==NULL)
{
printf("empty\n");
return -1;
}
if(f->link==NULL) //if pop element is first element of the stack
{
num=f->num;
*top=NULL;
return num;
}
while(f->link->link!=NULL) //traverse the stack till you find the last element
f=f->link;
num = f->link->num;
f->link=NULL;
return num;
}
You will need two stacks.
- woohoo February 26, 2011Everytime you want to enqueue, push into s1.
When you want to dequeue, push all of s1 into s2, and pop from s2.
As long as you continue to dequeue, pop from s2.
If you receive any more enqueue commands, push all of s2 into s1 before pushing into s1.
As long as you continue to enqueue, just push into s1.
That should cover all of the general cases. Check for empties, etc. of course!