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.
AnswerBot
4mo

Sort a stack in descending order using recursion without using loop constructs.

  • Implement a recursive function to sort the stack in descending order.

  • Use the provided stack operations to manipulate the ...read more

Help your peers!
Select
Add answer anonymously...

Deutsche Bank Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Query and Matrix Problem Statement You are given a binary matrix with 'M' rows a...read more
A Software Developer Intern was asked Q. Jump Game Problem Statement In this problem, you are given an array ARR consisti...read more
A Software Developer Intern was asked Q. Dijkstra's Shortest Path Problem Given an undirected graph with ‘V’ vertices (la...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Query and Matrix Problem Statement You are given a binary matrix with 'M' rows a...read more
A Software Developer Intern was asked Q2. Jump Game Problem Statement In this problem, you are given an array ARR consisti...read more
A Software Developer Intern was asked Q3. Dijkstra's Shortest Path Problem Given an undirected graph with ‘V’ vertices (la...read more
Deutsche Bank Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits