Sort Stack Problem Statement

You are given a stack S. Your task is to sort the stack recursively in descending order.

Constraints:
  • Looping through the stack is not allowed.
  • Return the stack sorted in descending order.

Example:

Input:
S = [1, 3, 2]
Output:
[3, 2, 1]
Explanation:

The stack after sorting would be from highest to lowest.

Input:

The first line contains an integer ‘T’ representing the number of test cases.
The second line of each test case contains a single integer ‘N’, the size of the stack.
The next line contains ‘N’ space-separated integers for the stack elements, where the last element is the top of the stack.

Output:

For each test case, output the stack sorted in descending order.
Each test case output should be on a separate line.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 2000
  • 0 <= S[i] <= 1000
Note:
You do not need to print anything. Your implementation should return the sorted stack.
AnswerBot
1d

Sort a given stack in descending order recursively without using loops.

  • Use recursion to pop elements from the stack and insert them in the correct position.

  • Maintain a temporary stack to hold elements ...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation 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