Yahoo Interview Question for Software Engineer / Developers






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

Are we allowed to instantiate one more stack ?
If yes, the problem is straightforward.

OrigStack = { 2, 6, 5, 1, 3}
NewStack = {NULL};

//Assume OrgiStack has atleast one element
NewStack.push(OrigStack.pop());

while(!OrigStack.isEmpty())
{
val = OrigStack.pop();
while(NewStack.top() < val) {
OrigStack.push(NewStack.top());
}
NewStack.push(val);
}

OrigStack = NewStack; //Done

Let me know suggestions. This the first trivial algo i can think about at the moment.

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

Couple of corrections:

OrigStack = { 2, 6, 5, 1, 3}
NewStack = {NULL};

//Assume OrgiStack has atleast one element
NewStack.push(OrigStack.pop());

while(!OrigStack.isEmpty())
{
val = OrigStack.pop();
while(!NewStack.isEmpty() && NewStack.top() < val) {
OrigStack.push(NewStack.pop());
}
NewStack.push(val);
}

OrigStack = NewStack; //Done

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

It seems correct. Just one quick comment.

If we can use an extra storage say the New Stack, we not use an array ? Then we can apply merge sort or quick sort, which has an average time complexity of O(nlogn). Faster than using another stack. Right?

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

To do this recursively we need to do the following:

1) Remove the top item
2) Reverse the remaining stack
3) Put the top item we removed at the bottom of the stack

2) Is a recursive call. Also, since we have no way of directly putting an item at the bottom of a stack, we can implement 3) recursively as well. The following would work:

import java.util.Stack;


public class StackReverse
{
    public void reverse(Stack stack)
    {
        if (!stack.isEmpty())
        {
            Object top = stack.pop();
            reverse(stack);
            moveItemToBottom(stack, top);
        }
    }


    private void moveItemToBottom(Stack stack, Object bottomItem)
    {
        if (stack.isEmpty())
        {
            stack.push(bottomItem);
        }
        else
        {
            Object o = stack.pop();
            moveItemToBottom(stack, bottomItem);
            stack.push(o);
        }
    }
}

- mattj777 at yahoo September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops, that was for the reverse a stack in place question. Posted it to the wrong thread.

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

Thanks mattj777! Actually in place sorting of stack can be done by modifying your solution of reverse stack!!
Instead of AddToBottom, we need to AddToCorrectLocation.

- anonymous September 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks mattj777, I wrote a stack sort program based on your idea.

void insert_item(stack<int> &s, int k) {
	if (s.empty())
		s.push(k);
	else {
		int j = s.top();
		if (j < k) {
			s.push(k);
		}
		else {
			s.pop();
			insert_item(s, k);
			s.push(j);
		}
	}
}

void stack_sort(stack<int> &s) {
	if (!s.empty()) {
		int k = s.top();
		s.pop();
		stack_sort(s);
		insert_item(s, k);
	}
}

int main() {
	stack<int> s;
	for (int i = 0; i < 10; ++i) {
		int k;
		cin >> k;
		s.push(k);
	}
	stack_sort(s);
	while (!s.empty()) {
		cout << s.top() << " ";
		s.pop();
	}
	cout << endl;
	return 0;
}

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

@ posix4e . Your code looks correct apart from newStack.Top() in the inner while loop should be newStack.Pop(); . Please let me know if I am wrong.

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

void sort(stack)
{
type x;

if (!isEmpty(stack)) {
x = pop(stack);
sort(stack);
insert(x, stack);
}
}

void insert(x, stack)
{
type y;

if (!isEmpty(stack) && top(stack) < x) {
y = pop(stack);
insert(x, stack);
push(y, stack);
} else {
push(x, stack);
}
}

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

brilliant.. just use & before stack in each function and it works fine!!!

code:

void insert(stack<int> &stk,int x)
{
     if(!stk.empty() && stk.top()>x)
     {
                     int y =stk.top();
                     stk.pop();
                     insert(stk,x);
                     stk.push(y);
                        
                        }
     else
     {
         stk.push(x);
         }
     
     
     }

void sort_stk(stack<int> &stk)
{
     if(!stk.empty())
     {
       int x=stk.top();
       stk.pop();
       cout<<x<<endl;
       sort_stk(stk);        
       insert(stk,x);
                       
       }
return;     
}

- anmolkapoormail September 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Sucharit ... newstack.top() will just get the reference value of the top node.it wont pop/remove the top node. To remove the top node we use the pop().In this case to compare it with "val",we dont need to pop the top node.

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

This is tower of hanoi problem

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

using two stacks

void SortStack(stack<int>& stk)
{
	int elem;
	stack<int> secStk;

	if(stk.empty())
		return;

	secStk.push(stk.top());
	stk.pop();
	while(!stk.empty())
	{
		elem = stk.top();
		stk.pop();
		if(elem >= secStk.top())
		{
			secStk.push(elem);
		}
		else
		{
			int data;
			while(!secStk.empty() && secStk.top() > elem)
			{
				data = secStk.top();
				secStk.pop();
				stk.push(data);
			}
			secStk.push(elem);
		}
	}

	while(!secStk.empty())
	{
		stk.push(secStk.top());
		secStk.pop();
	}
}

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

try recursive insersion sort, u need not 2 use an other stack...

void sort(stack s) {
    if (!IsEmpty(s)) {
        int x = Pop(s);
        sort(s);
        insert(s, x);
    }
}

void insert(stack s, int x) {
    if (!IsEmpty(s)) {  
        int y = Top(s);
        if (x < y) {
            Pop(s);
            insert(s, x);
            Push(s, y);
        } else {
            Push(s, x);
        }
    }
}

- amit.mnnit July 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

chutiyapa ...

- bond !!!!!! September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please maintain the sanity of this forum

- Anonymous August 14, 2011 | 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