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.
Be the first one to answer
Add answer anonymously...
Top Virtusa Consulting Services Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in Virtusa Consulting Services Associate Software Engineer
>
Virtusa Consulting Services Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app