Introduction
Reversing a stack is a classic data structures problem that frequently appears in coding interviews and competitive programming. While it may seem straightforward, the challenge lies in reversing the stack without using any additional data structure.
This problem is a great way to understand the power of recursion and stack manipulation.
In this article, we will explore an intuitive recursive approach, understand how it works step by step, and also briefly discuss alternative methods.
Link of Problem: GeeksforGeeks – Reverse a Stack
Problem Statement
You are given a stack st[]. The task is to reverse the stack.
Important Notes
- The input array represents the stack from bottom to top
- The last element is considered the top
- Output should display elements from top to bottom after reversal
Examples
Example 1
Input:
st = [1, 2, 3, 4]
Output:
[1, 2, 3, 4]
Explanation:
After reversing, the internal order becomes [4, 3, 2, 1],
so when printed from top → bottom, it appears as [1, 2, 3, 4].
Example 2
Input:
st = [3, 2, 1]
Output:
[3, 2, 1]
Explanation:
Reversed stack becomes [1, 2, 3] internally.
Key Insight
A stack only allows:
push()→ insert at toppop()→ remove from top
Challenge
We cannot directly insert an element at the bottom of the stack.
Solution Strategy
Use recursion to:
- Remove all elements from the stack
- Insert each element back at the bottom
This effectively reverses the stack.
Approach: Recursive Solution
Idea
- Pop the top element
- Reverse the remaining stack recursively
- Insert the popped element at the bottom
Code (Java)
Step-by-Step Dry Run
Let’s take:
Execution Flow:
- Pop 4 → reverse [1, 2, 3]
- Pop 3 → reverse [1, 2]
- Pop 2 → reverse [1]
- Insert elements at bottom in order
Final Stack:
Complexity Analysis
- Time Complexity: O(n²)
- Each insertion at bottom takes O(n)
- Auxiliary Space: O(n)
- Due to recursion call stack
Why This Approach Works
- Recursion helps access deeper elements of the stack
- The helper function inserts elements at the bottom
- Maintains order while reversing without extra structures
Alternative Approaches
1. Using Two Stacks
- Transfer elements between stacks multiple times
- Easy but uses extra space
2. Using Array/List
- Store elements in a list
- Push them back into stack
⚠️ These methods violate the constraint of not using extra data structures.
Key Takeaways
- Stack problems often require creative recursion
- Reversing a stack without extra space is a common interview pattern
- Understanding insert at bottom is crucial
- Time complexity is higher due to repeated insert operations
When This Problem Is Asked
This question is commonly seen in:
- Technical interviews
- Stack and recursion problem sets
- Platforms like GeeksforGeeks
It evaluates:
- Understanding of stack operations
- Recursive thinking
- Problem-solving under constraints
Conclusion
Reversing a stack without using extra space is a great example of how recursion can simplify complex constraints. While the solution may not be the most optimal in terms of time complexity, it is elegant and widely accepted in interviews.
Mastering this pattern will help you solve many similar problems involving stack manipulation.
Frequently Asked Questions (FAQs)
1. Why is the time complexity O(n²)?
Because inserting an element at the bottom takes O(n), and this is done for each element.
2. Can this be optimized further?
Not without using extra data structures. The recursive approach is optimal under constraints.
3. What is the key concept to learn here?
Understanding how to insert an element at the bottom of a stack using recursion.




