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.

Be the first one to answer
Add answer anonymously...
ZIGRAM Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter