Microsoft Interview Question
Software Engineer / DevelopersHere you are assuming queue is implemented using array. What if its implemented by linkedList.
#include <iostream>
#include <stdio.h>
#define max 5
using namespace std;
class queue
{
int front;
int rear;
int data[max];
public:
queue()
{
front = 0;
rear = 0;
}
void enQueue(int i)
{
if( rear == max )
return ;
data[rear++] = i;
}
int deQueue()
{
if( front == rear )
{
return -1;
}
return data[front++];
}
void disp()
{
cout<<"Front ="<<front<<" Rear = "<<rear<<endl;
}
int size()
{
return rear;
}
int reSet()
{
front = 0;
rear = 0;
}
};
class stack
{
queue q;
public:
void push( int i)
{
if( q.size() == max )
{
cout<<"Stack is full"<<endl;
return ;
}
q.enQueue(i);
}
int pop()
{
int ret = 0;
int result = 0;
queue temp;
int c = 0;
while( ( ret = q.deQueue() ) != -1)
{
temp.enQueue(ret);
result = ret;
}
q.reSet();
int size = temp.size();
if( size == 0 )
{
cout<<"stack is empty " <<endl;
return -1;
}
else
{
size--;
}
while(size--)
q.enQueue(temp.deQueue());
temp.reSet();
return result;
}
void disp()
{
q.disp() ;
}
};
1. Declare 2 Queues
2. Start inserting in Q1 till a pop is required
3. Transfer the contents of Q1 into Q2 except the last entry and pop it out
4. If another insertion in required insert in Q2 itself.
5. When another pop operation is required transfer the contents in Q1 back.
Nutshell: Whenever a pop is required transfer is required else keep inserting in the same queue.
<pre lang="" line="1" title="CodeMonkey97440" class="run-this">package com.DataStructures;
public class StackUsingQueue {
Queue enQueue=new Queue();
Queue deQueue=new Queue();
public void push(int item){
enQueue.insert(item);
}
public int pop(){
int item = 0;
while(!enQueue.isEmpty()){
item=enQueue.remove();
if(!enQueue.isEmpty()){
deQueue.insert(item);
}
}
// Swapping References
Queue tmp=enQueue;
enQueue=deQueue;
deQueue=tmp;
return item;
}
public static void main(String[] args) {
StackUsingQueue obj=new StackUsingQueue();
obj.push(10);
obj.push(30);
obj.push(50);
obj.push(70);
obj.push(100);
System.out.println(obj.pop());
System.out.println(obj.pop());
System.out.println(obj.pop());
}
}
</pre><pre title="CodeMonkey97440" input="yes">Implemented as mentioned by Ankit Garg above.</pre>
Version A: The stack should be efficient when pushing an item.
push:
enqueue in queue1
pop:
while size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2
dequeue and return the last item of queue1, then switch the names of queue1 and queue2
Version B:The stack should be efficient when popping an item
push:
enqueue in queue2
enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2
pop:
deqeue from queue1
I feel it can be done using 1 queue in the following way:
- Dan January 02, 20111. Push elements in queue Q and keep a count of total no of elements(n).
2. If element has to be popped, pop n-1 elements and push them again into the Q.
3. Now the element popped is the required element.
4. Update no of elements n and repeat the same steps to pop another element.
Any suggestions ?