AfterAcademy Tech
•
16 Mar 2020

Difficulty: Medium
Asked in: Microsoft, Adobe, Amazon, Goldman Sachs, Flipkart
Queue and Stack are both different data structures with different properties and also different use cases. However, the given problem is asking to implement one data structure, i.e., Queue, with the help of the other (Stack). Let us first look at the features and characteristics of Stack and Queue and try to get an idea of how we can implement them with the help of each other.
Stack and Queue

Conclusion
We can implement Queue using two Stacks. Two Stacks taken together can help us to get all the operations as supported by Queue. All the use-cases of queue can be achieved by using two stacks. Try it yourself first!
Possible questions to ask the interviewer: →
We are going to discuss two different approaches to this problem. Both the approaches, however, will require two stacks to be used. There is a third solution also, which is a little bit trickier, we will discuss it at the end with more details.
Solution idea
The idea here is to modify the insertion operation of the Queue so that it can be implemented using Stacks. Since in a queue the element which gets in first, gets out first so when using stack we have to figure out a way so that the element inserted first should present at the top of the stack. For this during insertion, we will use a second stack to push everything from the first stack into the second one and then push the element to be inserted into the first one then again all the elements from the second stack will be pushed to the first one. This way the insertion is costly but we can get a queue using stack.
Solution steps
Algorithm for Insertion
Algorithm for Deletion
Pseudo-code
class queueUsingStack
{
stack <datatype> S1, S2
void enQueue(int data)
{
while(S1 is not Empty)
{
int topElement = S1.top()
S1.pop()
S2.push(topElement)
}
S1.push(data)
while(S2 is not empty)
{
int topElement = S2.top()
S2.pop()
S1.push(topElement)
}
}
int deQueue()
{
if(S1 is empty)
return -1
int topElement= S1.top()
S1.pop()
return topElement
}
Complexity Analysis
Time Complexity: O(N) for Insertion, O(1) for Deletion.
Space Complexity: O(N)
Critical ideas to think!
Solution idea
Here we modifying the deletion or the dequeue operation of the queue. This will lead to the dequeue operation being more costly but it will help in implementing queue with stacks. For deleting we first need to check if both the stacks(as we are using two) are not empty and if this condition is true, we will simply move all the elements from stack S1 to stack S2. This will let the first element inserted into the stack to be at the top. We will then perform a pop operation on stack S2.
Solution steps
Algorithm for Insertion
Algorithm for Deletion
Pseudo-code
class queueUsingStack
{
stack <datatype> S1, S2
void enQueue(int data)
{
S1.push(data)
}
int deQueue()
{
if( S1 is empty and S2 is empty)
return -1
while(S1 is not empty)
{
int topElement = S1.top()
S2.push(topElement)
S1.pop()
}
int topElement = S2.top()
S2.pop()
return topElement
}
}
Complexity Analysis
Time Complexity: O(N) for dequeue and O(1) for enqueue.
Space Complexity: O(N) (Why?)
Solution idea
The idea of this solution is very obvious but does not comes to mind very often. In the all above solutions, we are using two stacks, here in this solution we will use one stack and the other one is the recursion call stack. This idea can be used in both the above methods. We are going to discuss the implementation of the second method using this.
Solution steps
Algorithm for Insertion
Algorithm for Deletion
Pseudo-code
class queueUsingStack
{
stack <datatype> S1
void enQueue(int data)
{
S1.push(data)
}
int deQueue()
{
if(S1 is empty)
return -1
int topElement = S1.top()
S1.pop()
if(S1 is empty)
return topElement
int result = deQueue()
S1.push(topElement) //reversing the stack
return result
}
}
Complexity Analysis
Time Complexity: O(1) for Insertion, O(N) for Deletion.
Space Complexity: O(N)
Critical ideas to think!

Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
Given a stack, you have to sort it in ascending order. You can use another stack if you need auxiliary space. The problem is asked in Google, Microsoft and Amazon and requires knowledge of Stack and recursion to solve it.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft, Yahoo and Adobe. The problem is to design a stack that will support push(), pop(), top() and getMin() operation in constant time. We will be discussing two different ways to approach this problem.

AfterAcademy Tech
Given a stack of integers st, write a program to reverse the stack using recursion. This problem will clear the concept of recursion.

AfterAcademy Tech
Sort a linked list using insertion sort and return its head.
