Sort a Stack Problem Statement
You are given a stack. Your task is to sort the elements within the stack in ascending order using only push and pop operations. You are allowed to use one additional stack for assistance.
Example:
Input:
Input Stack : 5 (top)
4
3
2
1
Output:
Output Stack : 1 (top)
2
3
4
5
Input:
First line of input consists of an integer representing the size of the stack, N
Second line of input consists of N space-separated integers representing the elements of the stack
Output:
The only line of output consists of the elements of the stack in sorted order (space-separated)
Constraints:
- 0 <= N <= 1000
- Time limit: 1 sec
Note:
Implement the function to sort the stack. You are not required to print anything; it has already been taken care of.

AnswerBot
4mo
Sort a given stack in ascending order using only push and pop operations with the help of an additional stack.
Use one additional stack to assist in sorting the elements.
Pop elements from the input sta...read more
Help your peers!
Add answer anonymously...
ZIGRAM Software Developer interview questions & answers
A Software Developer was asked Q. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more
A Software Developer was asked Q. Sort a Stack Problem Statement You are given a stack. Your task is to sort the e...read more
A Software Developer was asked Q. Hourglass Pattern Problem Statement Given an integer 'N', print an hourglass pat...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more
A Software Developer was asked Q2. Sort a Stack Problem Statement You are given a stack. Your task is to sort the e...read more
A Software Developer was asked Q3. Hourglass Pattern Problem Statement Given an integer 'N', print an hourglass pat...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

