Microsoft Interview Question for Software Engineer in Tests


Team: MSIT
Country: United States




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

PUSHBOTTOM()
1.pop all the elements from stack A into stack B.
2.push the element in to stack A.
3. Push back all the elements one by one in to statck A from stack B

POPBOTTOM()
1.pop all the elements from stack A into Stack B except the last element.
2.Now pop the bottom element .
3.push back similarly from stack B to stack A.

- Dinesh March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the 3rd step can be avoided, most of the times, in both the method, as transferring elements again and agains etween stack for every operation is a costly affair. Rather one can avoid it, by putting condition like...

Void PushBottom()
{
if (!A.isEmpty()) --> transfer those elements to B, if B has capacity to accomodate

If ( A.isEmpty() ) -> push the new element at bottom


}

int PopBottom()
{
if( ! A.isEmpty() ) --> Return the only element present, which is BootomMost in A

else if (B is not Empty) --> return the topmost element from B; (as it will become bottom most if you transfer it to A);

}

This way one can avoid transfers of elements between 2 stacks and do it only when it is necessary.

- Jayesh V May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think push will be the same , push the element in stack A , Now for pop , first pop all the elements from stack A and push them to stack B, and return first element of the stack B.

- Anonymous March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, Push will be different...If you push normally...It will go to the top of the stack A not bottom

- Maxxx June 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ReverseStack
    {
        private Stack _stackA = new Stack();
        private Stack _stackB = new Stack();

        public void PushBottom(object value)
        {
            while(!_stackA.IsEmpty())
            {
                _stackB.Push(_stackA.Pop());
            }
            _stackA.Push(value);
            while(!_stackB.IsEmpty())
            {
                _stackA.Push(_stackB.Pop());
            }
        }

        public Node PopBottom()
        {
            while (!_stackA.IsEmpty())
            {
                _stackB.Push(_stackA.Pop());
            }
            var node = _stackB.Pop();
            while (!_stackB.IsEmpty())
            {
                _stackA.Push(_stackB.Pop());
            }
            return node;
        }
    }

- Artur March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
node *next;
};

void push(struct node **top, int data) {
//struct node *t = top;
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = *top;
*top = newnode;
}

int IsEmpty(struct node *top) {
return top == NULL;
}

int pop(struct node **topA,struct node **topB) {
struct node *t = NULL;
while(!IsEmpty(*topA)) {
t = *topA;
//t = t->next;
push(&*topB,t->data);
*topA = t->next;
}
return t->data;
}

void pushbottom(struct node **topA,struct node **topB,int data) {
pop(&*topA,&*topB);
push(&*topA,data);
pop(&*topB,&*topA);
}

int popbottom(struct node **topA,struct node **topB) {
pop(&*topA,&*topB);
struct node *t = *topB;
*topB = t ->next;
pop(&*topB,&*topA);
return t->data;
}

int main() {
struct node *topA = NULL;
struct node *topB = NULL;
push(&topA,1);
push(&topA,2);
push(&topA,3);
//pop(&topA,&topB);
pushbottom(&topA,&topB,0);
printf("%d\n",popbottom(&topA,&topB));
while(topA!=NULL) {
printf("%d\n",topA->data);
topA = topA->next;
}
}

- Dinesh March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess we don need to empty any stack..aur transfer element just take two flags :insertion flag and another deletion flag...all will happen alternately..thats it 1 for stack a and one for stack b

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

template<typename T>
void pushBottom(T x)
{
         if (s.empty()) s.push(x);
         else {
                  Top t = s.top(); s.pop();
                   pushBottom(x);
                   s.push(t);
         }
}

template<typename T>
T popBottom()
{
        T top = s.top(); s.pop();
         if (s.empty()) return T;
         T ans = popBottom();
          s.push(top);
          return ans;
}

- Anonymous August 06, 2013 | 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