Sort a Stack Problem Statement

You are provided with a stack consisting of 'N' integers. The goal is to sort this stack in descending order by utilizing recursion.

Allowed Stack Operations:

is_empty(S) : Check if the stack is empty.
push(S) : Add a new element to the stack.
pop(S) : Remove the top element from the stack.
top(S) : Get the value of the top element without removing it.

Input:

The initial input line includes the integer 'T', which indicates the number of test cases.
Each individual test case is composed of:
- The first line: an integer 'N' indicating the number of elements within the stack.
- The following line: 'N' space-separated integers representing the stack's elements.

Output:

The output for each test case must consist of 'N' space-separated integers presenting the stack after it has been sorted in descending order.

Example:

Input:
2
3
3 2 1
5
4 5 3 2 1
Output:
3 2 1
5 4 3 2 1

    Constraints:

  • 1 <= 'T' <= 100
  • 1 <= 'N' <= 100
  • 1 <= |'V'| <= 109 (where |V| signifies the absolute value of any stack element)
  • Use of loop constructs such as while or for is prohibited.
  • Duplicate integers may exist within the stack.
  • Stack elements can be negative, positive, or zero.
  • Time limit: 1 sec
Note:
You are not required to print anything; the output is handled by the system. Concentrate on implementing the function.
Be the first one to answer
Add answer anonymously...
Deutsche Bank Software Developer Intern 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