Adobe Interview Question for Computer Scientists


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
11
of 11 vote

void insert_at_bottom(node **stack, int data)
{
     if( isempty(*stack) ){
          push(stack,data);
          return;
     }
     int temp=pop(stack);
     insert_at_bottom(stack,data);
     push(stack,temp);
}  


void rev_stack(node **stack)
{
     if( isempty(*stack) ) return;
     int temp = pop(stack);
     rev_stack(stack);
     insert_at_bottom(stack,temp);
}

- mandeep April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect !!!

- Vignesh May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I guess we are ending up using more space here i.e. system stack.

- Anonymous March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It goest to an infinite loop. why ?

- hh October 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

//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);
    }
}

- googlebhai February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Solution........

- Siva February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong solution

- abc February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't the call stack a stack?

- Anonymous February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect... I did execute the program in Java. It works great

- srigurubelli May 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Using recursion involves using extra space.

- eugene.yarovoi February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- gimli February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Dev February 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

- Dev February 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gimli, How can you do it without recursion using two stacks? I guess you need three of them. Isn't it a tower's of hanoi kind of problem? Let me know.

- guest123 March 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

- mag February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+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).

- Nishant Kelkar January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First, think about how to add a data to the bottom of the a stack, which requirement a recursive function. If you can sort out the 1st question, then it is straight forward to use another recursive function to revert a stack.

- Ric February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using pointers

pt1 = First;
pt2 = Last;

pt1 = pt1+pt2;
pt2= pt1- pt2;
pt1= pt1- pt2;

- Anonymous February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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....

- soumyajuit April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I've the same solution.Can anyone suggest wat can be wrong in this?

- sidd.scope July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int size = stack.size() - 1;
		
		for (int i = 0 ; i < stack.size() / 2 ; i++) {
			String temp = (String) stack.get(i);
			stack.set(i, stack.get(size));
			stack.set(size, temp);
			size--;
		}

- Div April 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Nitin Gupta iitian June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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) ;

- Nitin Gupta iitian June 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Sridhar Kondoji March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code does not reverse stack contents. Instead, keeps the content as is.

- Amar July 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}
}

- Anonymous February 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
        }
    }
}

- maheshbhosale24051996 June 05, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More