Reverse Stack with Recursion

Reverse a given stack of integers using recursion. You must accomplish this without utilizing extra space beyond the internal stack space used by recursion. Additionally, you must refrain from using any loop constructs like for, for-each, while, etc.

Input:

The first line of input includes an integer 'N' indicating the size of the stack.
The second line provides 'N' space-separated integers signifying the elements of the stack, where the last (Nth) element is on top of the stack.

Output:

N space-separated integers, representing the reversed stack, with the first element being the new top of the stack.

Example:

Input:
3
2 1 3
Output:
3 1 2
Explanation:

The stack of size 3 is provided as input with elements 2, 1, and 3 from bottom to top respectively. The reversed output would have the top element as 3, followed by 1 and 2.

Constraints:

  • 0 ≤ N ≤ 103
  • Time Limit: 1 sec

Note:

You are only allowed to use the following inbuilt stack methods:
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
AnswerBot
4mo

Reverse a given stack of integers using recursion without using extra space or loop constructs.

  • Use recursion to pop all elements from the original stack and store them in function call stack.

  • Once the ...read more

Help your peers!
Select
Add answer anonymously...

Virtusa Consulting Services Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 3mo agoQ. How do you use actuators in Spring Boot?
An Associate Software Engineer was asked 3mo agoQ. What is the process of exception handling in Spring Boot?
An Associate Software Engineer was asked 3mo agoQ. Explain the OOPS concepts.

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 3mo agoQ1. How do you use actuators in Spring Boot?
An Associate Software Engineer was asked 3mo agoQ2. What is the process of exception handling in Spring Boot?
An Associate Software Engineer was asked 3mo agoQ3. Explain the OOPS concepts.
Virtusa Consulting Services Associate Software Engineer 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