AfterAcademy Tech
•
06 Oct 2020

Difficulty: Easy
Asked in: Yahoo, Amazon, Adobe
Problem Description
Given a stack of integers st, write a program to reverse the stack using recursion.
Problem Note
st after reversing it using recursion. It is mandatory to reverse st using recursion.Example 1
Input: st = [1, 5, 3, 2, 4]
Output:[4, 2, 3, 5, 1]
Explanation: After reversing the stack [1, 5, 3, 2, 4] becomes [4, 2, 3, 5, 1].
Example 2
Input: st = [5, 17, 100, 11]
Output: [11, 100, 17, 5]
Explanation: After reversing the stack [5, 17, 100, 11] becomes [11, 100, 17, 5]
The problem name itself addresses the solution to be a recursive solution. To understand what is a recursive solution, please refer here.
You must have known that a stack usually comprises two operations, i.e. push and pop operations on the top of the stack. So, If we have to reverse a stack, then we have to pop the elements of the stack and push it to another auxiliary stack one by one. In this way, the auxiliary stack will be the same stack in reverse order.
But we are not looking to use an auxiliary stack, but we can use recursion. And a recursive function behaves like a stack. So, What we are going to do that is we can create a recursive function and pop all stack items and stores the popped item in the function call stack. And when the input stack becomes empty, we push the new item stored in the call stack at the bottom of the input stack.
You can practice this problem here.
In short, we will take out the top element of the input stack and store it in the recursion stack until the stack becomes empty and while returning to the previous state of recursion, we push the stored element in each recursion stack at the bottom of the input stack.
Look at the below example:

Solution Steps
recur to reverse the stack.Pseudo Code
int insert_at_bottom(int x, stack st) {
if(st is empty) {
st.push(x)
}
else {
int a = st.top()
st.pop()
insert_at_bottom(x)
st.push(a)
}
}
void reverse(stack st) {
if(st is not empty) {
int st = st.top()
st.pop()
reverse(st)
insert_at_bottom(x, st)
}
}
// driver function
int[] reverseStack(int[] st) {
reverse(st)
return st
}
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(n)
Critical Ideas To Think
Please comment down below if you have a better insight in the above approach.
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 Microsoft, Amazon, Adobe, Flipkart, etc. Here, we need to implement Queue data structure using another data structure Stack. This problem is meant for testing the data structure knowledge.

AfterAcademy Tech
Stack is a very useful concept that a good programmer could use for their benefit. We will be discussing the basic design of stack and the operations that can be performed on it. We shall be also discussing the implementation of stack.

AfterAcademy Tech
Using recursion, certain problems can be solved quite easily. Several algorithms design techniques and data structures are based on recursive thinking.
