Yahoo Interview Question for Software Engineer / Developers






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

reverse(struct node* stack)
{
int x;
while(!IsEmpty(stack))
{
x = pop(stack);
reverse(stack);
swap(stack, x);
}
}

void swap(struct node* stack, int x)
{
if(!IsEmpty(stack))
{
int y = pop(stack);
swap(stack, x);
push(stack,y);
}
else
{
push(stack,y);
}
}
}

- PrateekCaire September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <stack>
#include <iostream>

using namespace std;

typedef stack<int> Stack;

void push_to_bottom(Stack& stack, int x);

void reverse(Stack& stack)
{
   if (!stack.empty()) {
      int x = stack.top();
      stack.pop();
      reverse(stack);
      push_to_bottom(stack, x);
   }
}

void push_to_bottom(Stack& stack, int x)
{
   if (stack.empty()) {
      stack.push(x);
   }
   else {
      int y = stack.top();
      stack.pop();
      push_to_bottom(stack, x);
      stack.push(y);
   }
}

// destructive
void dump_stack(Stack& s)
{
   while (!s.empty()) {
      int i = s.top();
      s.pop();
      cout << i << ' ';
   }
   cout << endl;
}

int main()
{
   Stack s;

   // 5 at bottom
   s.push(5);
   s.push(4);
   s.push(3);
   s.push(2);
   s.push(1);

   reverse(s);

   dump_stack(s);
}

jd@shade:/mnt/raid/projects/interviewQs$ ./swap-stack-recursively
5 4 3 2 1

Prateek you had it apart from small errors.

If we were allowed to use 'size()', we could have done a loop at the top, popping off the top, and pushing it to the bottom, size() times. But this way it's pure recursion.

- JeffD September 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse( stack S )
{
     
    if( S.isEmpty() )
     return ;
     
    int data = S.top() ;
    
    S.pop();
    
    reverse( S );
    
    Insert( S , data );
    
}


void Insert( stack S , int data )
{
     if( S.isEmpty() )
      {
         S.push( data );
         return;
         
         }
         
      int X=S.top();
      S.pop();
      
      Insert( S , data );
      S.push( X );
      
      }

- JD September 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void returnSorted(stack &s)
{
	if(s.IsEmpty)
	    return; 
	else
	{
		int val = stack.pop();
		
		returnSorted(s);

		appendToSorted(val, s);		
	
	}
}

void appendToSorted(int val, stack s)
{
	if (s.IsEmpty())
		s.push(val);
	else
	{
		int temp = s.top();
		
                if (val > temp)
		{
			appendToSorted(val, s.pop());
		
			s.push(temp);				
		}
                else
		{
			if(!s.isFull())
				s.push(val);
		}

	}
}

- Anonymous September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the following should work... could someone comment?

void stackRec(Node* lastPopped) {
  if (!isEmpty()) {
    stackRec(pop());
  }
  push(lastPopped);
  return;
}

int main() {
  stackRec(NULL);
  return 0;
}

- jiazou October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nvm... just realized it didn't do anything... that was stupid...

- jiazou October 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's really easy one

import java.util.*;
class StackRev
{
	public static void main(String args[])
	{
		Stack<Integer> s=new Stack<Integer>();
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter the no. of no.s to be pushed ");
		int n=sc.nextInt();
		for(int i=0;i<n;i++)
		{
			System.out.println("Enter");
			s.push(sc.nextInt());
		}
		reverse(s);
		System.out.println("Stack after reversal is ");
		while(!s.isEmpty())
		System.out.println(s.pop());
	}
	public static void reverse(Stack<Integer> s)
	{
		if(s.isEmpty())
		return;
		int x=s.pop();
		reverse(s);
		s.push(x);
	}
}

- gullu October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am sorry the above one is wrong

- gullu October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ReverseStack(stack<int>& stk)
{
	if(stk.empty())
		return;

	int elem = stk.top();
	stk.pop();
	ReverseStack(stk);
	Reverse(stk, elem);
}

void Reverse(stack<int>& stk, int elem)
{
	if(stk.empty())
		stk.push(elem);
	else
	{
		int data = stk.top();
		stk.pop();
		Reverse(stk, elem);
		stk.push(data);
	}
}

- Anonymous July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

We can use another temp stack to store the elements pop from the original stack, this shouldn't be regarded as using extra memory ( except constant pointer overhead )

public void ReverseStack(Stack* s1)
{
// create an empty stack
Stack* s2 = new Stack();
ReverseStackRecur(s1,s2);
}

public void ReverseStackReCur(s1, s2)
{
if((s1 == null)||(s1.IsEmpty))
{
return;
}
s2.Push(s1.Pop());
ReverseStackReCur(s1,s2);
s1.Push(s2.Pop());
}

- Nat September 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its already stated that we cannot use the extra data structure but you are using a new stack here?

- DashDash September 13, 2010 | Flag


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