Adobe Interview Question
Computer ScientistsCountry: India
Interview Type: In-Person
//from stackoverflow, but there is temp storage is involved in appendStack
public static void revertStack(Stack<Integer> s) {
if (s.isEmpty()) {
return;
} else {
Integer a = s.pop();
revertStack(s);
appendStack(s, a);
}
}
public static void appendStack(Stack<Integer> s, Integer a) {
if (s.isEmpty()) {
s.push(a);
return;
} else {
Integer o = s.pop();
appendStack(s, a);
s.push(o);
}
}
agree. Kind of stupid question.
In recursion you are using program stack to hold the popped values.
Without recursion you can achieve the same using another stack.
One point friends...I dont think this is a stupid question. When the interviewer asks for a recursive solution, it does not mean that it is the best solution (in terms of time and space). The purpose of the question is to understand the candidates knowledge of recursion and how well he can think recursively..including double recursion.
And yes..anyone who is familar with recursion will understand that all recursive functions use a stack internally, but what the interviewer wants is to avoid implicit stack creation.
With O(n) extra space it is trivial.
Without extra space, you need recursion to hold values. You can ask the interviewer that if I have source code of given stack, if I have the information on how the stack is implemented, then reverse that internal data structure of stack for example, array and linked lists can be reversed using constant extra space.
+1 I know this is an old question, but I just thought I should mention that this seems to be a good approach. Finding the internal structure and using it is much more easier (and correct) to achieve the desired stack reversal in O(1) rather than using double recursion (which uses an internal function stack anyway).
I think this would be implementation depended...
if it is normal array then putting two pointer one from end and one from start and swapping them would reverse the stack...
for link list reverse would be fine I guess....
we use recursion ; i.e. System Stack called SS
and our stack being S;
Algo: 1> first pop all the element of stack S and push it into SS s.t. data is the top of the data of SS
2> now We Pop element of SS ( done by automatically when recursion back up) and push element in S s.t. if it being first push then done it otherwise again pop Element of S until empty and push into SS then insert that element and again push element of SS to S
void ReverseStack ( struct Stack *S ) { int data ;
if ( ! ISEmpty(S) ) return ;
data = POP(S) ; // collecting top most data of SS
ReverseStack(S) ; // popping from S and pushing in SS
InsertInStack(S,data); // this will insert into stack
}
void InsertInStack(struct Stack *S, int data)
{ if ( ! ISEmpyt(S) ) { PUSH(S,data); return ; } // if it being first element
int temp;
temp = POP(S) ; InsertInStack(S,data) ; // pop all element of the Stack and push into SS and then again push PUSH(S,temp) ; //in S
we use recursion ; i.e. System Stack called SS
and our stack being S;
Algo:
1> first pop all the element of stack S and push it into SS s.t. data is the top of the data of SS
2> now We Pop element of SS ( done by automatically when recursion back up) and push element in S s.t. if it being first push then done it otherwise again pop Element of S until empty and push into SS then insert that element and again push element of SS to S
void ReverseStack ( struct Stack *S ) {
int data ;
if ( ! ISEmpty(S) ) return ;
data = POP(S) ; // collecting top most data of SS
ReverseStack(S) ; // popping from S and pushing in SS
InsertInStack(S,data); // this will insert into stack
}
void InsertInStack(struct Stack *S, int data)
{
if ( ! ISEmpyt(S) ) { PUSH(S,data); return ; } // if it being first ele
int temp;
temp = POP(S) ; InsertInStack(S,data) ; // pop all element of
//the Stack and push into SS and then again push in S
PUSH(S,temp) ;
Hi,
I am commenting on a very old thread. Not sure if iam missing something in the question, but using a stack in a single recursive loop can reverse the stack elements. Stack ADT itself can be considered as first level recursion and the second recursion that we do in code together can be treated or assumed as double recursion.
Here is my sample code that worked in unit test.
@Test
public void reverseStack() {
Stack<String> stack = new Stack<String>();
stack.push("a");
stack.push("b");
stack.push("c");
reverseStackFunc(stack);
for (String s : stack) {
System.out.println(s);
}
}
private void reverseStackFunc(Stack<String> stack) {
if (stack.empty()) {
return;
}
else {
String s = stack.pop();
reverseStackFunc(stack);
stack.push(s);
}
}
Thanks
sri
public class ReversingStack<T>
where T:IComparable<T>
{
public Stack<T> Reverse(Stack<T> stack)
{
if(stack.Count>0)
{
/* Hold all items in Function Call Stack until we reach end of
the stack */
T temp = stack.Pop();
stack= Reverse(stack);
/* Insert all the items (held in Function Call Stack) one by one
from the bottom to top. Every item is inserted at the bottom */
stack = InsertAtBottom(stack, temp);
}
return stack;
}
private Stack<T> InsertAtBottom(Stack<T> stack, T item)
{
if (stack.Count == 0)
{
stack.Push(item);
}
else
{
/* Hold all items in Function Call Stack until we reach end of
the stack. When the stack becomes empty, the isEmpty(*top_ref)
becomes true, the above if part is executed and the item is
inserted at the bottom */
T temp = stack.Pop();
stack = InsertAtBottom(stack, item);
/* Once the item is inserted at the bottom, push all the
items held in Function Call Stack */
stack.Push(temp);
}
return stack;
}
}
import java.util.Stack;
class GFG {
public static void main (String[] args) {
Stack<Integer> s = new Stack();
s.push(4);
s.push(3);
s.push(2);
s.push(1);
System.out.println("Original stack");
System.out.println(s);
reverse(s);
System.out.println("Reversed stack");
System.out.println(s);
}
public static void reverse(Stack<Integer> s) {
if (s.isEmpty()) {
return;
}
int pop = s.pop();
reverse(s);
insert(s, pop);
}
public static void insert(Stack<Integer> s, int popped) {
if (s.isEmpty()) {
s.push(popped);
} else {
int element = s.pop();
insert(s, popped);
s.push(element);
}
}
}
- mandeep April 22, 2012