Microsoft Interview Question for Software Engineer in Tests






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

You will need two stacks.

Everytime you want to enqueue, push into s1.
When you want to dequeue, push all of s1 into s2, and pop from s2.
As long as you continue to dequeue, pop from s2.
If you receive any more enqueue commands, push all of s2 into s1 before pushing into s1.
As long as you continue to enqueue, just push into s1.

That should cover all of the general cases. Check for empties, etc. of course!

- woohoo February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A little optimization in above (It's NEVER required to move elements from s2 to s1. Also move from s1 to s2 is needed only when s2 is EMPTY)
1. Have two stacks, s1 and s2.
2. If Enqueue, push into s1
3. If Dequeue, pop from s2 (if s2 is not empty). If s2 is empty, move all elements from s1 to s2 (pop from s1 and push in s2). Now pop from s2.

Enqueue Cost: O(1)
Dequeue Cost: O(1) -- Amortized

- Anurag Singh February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ah good call. Thank you for that!

- woohoo February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome approch...!

- PKT February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

WOW.. Appreciated. Good logic.

If anybody publish some code then that will really help.

- sidhartha.mahapatro February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

WOW.. Appreciated. Good logic.

If anybody publish some code then that will really help.

- sidhartha.mahapatro February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you tell me what's the optimization in anurag singh's answer over nugarp's.

- Lavanya March 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I had additional popping when not necessary

- nugarp March 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anurag Singh, good approach!!!!

- Searching April 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class QeueUsingTwoStacks
    {
        Stack pushStack = new Stack();
        Stack popStack = new Stack();
        public void Enqueue(int data)
        {
            Console.WriteLine("Enqueued:{0}", data);
            pushStack.Push(data);
        }
        public SinglyNode Dequeue()
        {
            if (popStack.isEmpty())
            {
                while (!pushStack.isEmpty())
                    popStack.Push(pushStack.Pop().data);
            }
            return popStack.Pop();
        }
    }

- Anonymous February 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome! Thanks for the elegant code snippet.

- sidhartha.mahapatro February 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

const int Max = 10;

class stack
{

public:
	int arr[Max],top;
	stack();
	void push(int item);
	int pop();
};

stack::stack()
{
	top = -1;
}

void stack::push (int data)
{
	if (top == Max-1)
	{
		cout << "\n the stack is full";
		return;
	}

	top++;
	arr[top] = data;
}

int stack::pop()
{
	if (top == -1)
	{
		cout << "\n the stack is empty";
		return NULL;
	}
	int data = arr[top];
	top--;
	return data;
}

void main()
{
	stack *S1,*S2;

	S1 = new stack();
	S2 = new stack();
	
	
	void enqueue (stack *S1, int item);
	int dequeue(stack *S1, stack *S2);
	void displayQueue(stack *S1,stack *S2);
	
	enqueue(S1,1);
	enqueue(S1,2);
	enqueue(S1,3);
		
	displayQueue(S1,S2);
	
	dequeue(S1,S2);

	enqueue(S1,4);
	enqueue(S1,5);

	displayQueue(S1,S2);

	dequeue(S1,S2);
	dequeue(S1,S2);
	dequeue(S1,S2);
	dequeue(S1,S2);
	
	displayQueue(S1,S2);

	enqueue(S1,6);
	enqueue(S1,7);
	enqueue(S1,8);
	
	displayQueue(S1,S2);

	dequeue(S1,S2);

	displayQueue(S1,S2);
	
}

void enqueue(stack *S1,int item)
{
	S1->push (item);
}

int dequeue(stack *S1, stack *S2)
{
	
	if (S2->top == -1)
	{
		for (int i=S1->top;i>-1;i--)
		{
			S2->push(S1->pop());
		}
	}
	int data = S2->pop();
	cout << "\n the dequeued element is" << data;
	return (data);
}

void displayQueue(stack *S1,stack *S2)
{
	if (S2->top ==-1)
	{
		if (S1->top == -1)
		{
			cout << "\n the Queue is empty";
			return;
		}
		else
		{
			for (int i=0;i<=S1->top;i++)
			{
				cout << "\n the" << i+1<<"th element is"<<S1->arr[i] ;
			}
		}
	}

	else
	{
		for (int j=S2->top;j>-1;j--)
		{
			cout << "\n the" << S2->top-j+1<<"th element is"<<S2->arr[j] ;
		}
		for (int k=S1->top;k>-1;k--)
		{
			cout << "\n the" << S2->top+2+S1->top-k << "th element is" << S1->arr[S1->top-k];
		}
	}
}

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

Using only one stack

struct node {
       int num;
       struct node *link;
       };
       
       typedef struct node node1;
       
       
       struct node *getnode()
       {
              
       return (struct node*)malloc(sizeof(struct node));       
       }
       
       void Display(struct node *p)
       {
       while(p)
       {
       printf("%d \n",p->num);
       p=p->link;        
       }     
            
       }
       
       void QInsert(node1 **top,int num)
       {
       node1 *temp = getnode();
       
       temp->num=num;
       temp->link=*top;
       *top=temp;    
           
       }
       
       int QDelete(node1 **top)
       {
       node1 *f=*top;
       int num;
       if(*top==NULL)
       {
       printf("empty\n");
       return -1;              
       }    
       if(f->link==NULL)                         //if pop element is first element of the stack
        {
        num=f->num;
        *top=NULL; 
        return num;               
        }
       while(f->link->link!=NULL)                //traverse the stack till you find the last element
        f=f->link;
       
        num = f->link->num;
        f->link=NULL;
        return num;
       
       
       }

- msankith October 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pleasss help?
what's the complecsity of queue using 2 stacks

- nitafa September 21, 2012 | 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