Microsoft Interview Question for Software Engineer / Developers






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

I feel it can be done using 1 queue in the following way:

1. Push elements in queue Q and keep a count of total no of elements(n).
2. If element has to be popped, pop n-1 elements and push them again into the Q.
3. Now the element popped is the required element.
4. Update no of elements n and repeat the same steps to pop another element.

Any suggestions ?

- Dan January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it will be 2 queue.
where will you keep n-1 element?

- pankaj January 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one !! U r pushing them back in the same queue..

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

Here you are assuming queue is implemented using array. What if its implemented by linkedList.

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

good one !! we assume unlimited array size

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

The n-1 elements you pop, need to be kept temporarily in another queue

- Tannu November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <iostream>
#include <stdio.h>
#define max 5

using namespace std;

class queue
{

int front;
int rear;
int data[max];
public:
queue()
{
front = 0;
rear = 0;

}
void enQueue(int i)
{
if( rear == max )
return ;

data[rear++] = i;
}
int deQueue()
{
if( front == rear )
{
return -1;
}
return data[front++];
}
void disp()
{
cout<<"Front ="<<front<<" Rear = "<<rear<<endl;
}
int size()
{
return rear;
}
int reSet()
{
front = 0;
rear = 0;
}

};

class stack
{
queue q;
public:
void push( int i)
{
if( q.size() == max )
{
cout<<"Stack is full"<<endl;
return ;
}
q.enQueue(i);
}
int pop()
{
int ret = 0;
int result = 0;
queue temp;
int c = 0;
while( ( ret = q.deQueue() ) != -1)
{
temp.enQueue(ret);
result = ret;
}

q.reSet();
int size = temp.size();
if( size == 0 )
{
cout<<"stack is empty " <<endl;
return -1;
}
else
{
size--;
}

while(size--)
q.enQueue(temp.deQueue());

temp.reSet();

return result;
}
void disp()
{
q.disp() ;
}
};

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

use 2 queues for stack pop operation..

- sekhar740 November 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

one queue is enough.

- jiangok November 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Declare 2 Queues
2. Start inserting in Q1 till a pop is required
3. Transfer the contents of Q1 into Q2 except the last entry and pop it out
4. If another insertion in required insert in Q2 itself.
5. When another pop operation is required transfer the contents in Q1 back.

Nutshell: Whenever a pop is required transfer is required else keep inserting in the same queue.

- Ankit Garg December 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we just use a double ended queue ?

- SH February 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey97440" class="run-this">package com.DataStructures;

public class StackUsingQueue {

Queue enQueue=new Queue();
Queue deQueue=new Queue();

public void push(int item){
enQueue.insert(item);
}

public int pop(){
int item = 0;
while(!enQueue.isEmpty()){
item=enQueue.remove();
if(!enQueue.isEmpty()){
deQueue.insert(item);
}
}

// Swapping References
Queue tmp=enQueue;
enQueue=deQueue;
deQueue=tmp;

return item;
}

public static void main(String[] args) {

StackUsingQueue obj=new StackUsingQueue();
obj.push(10);
obj.push(30);
obj.push(50);
obj.push(70);
obj.push(100);

System.out.println(obj.pop());
System.out.println(obj.pop());
System.out.println(obj.pop());

}

}
</pre><pre title="CodeMonkey97440" input="yes">Implemented as mentioned by Ankit Garg above.</pre>

- akash.kotadiya2000@gmail.com June 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To understand implementing a Queue from Stack, check this video in YouTube.

Search for "How to implement a Queue" directly in YouTube.

- Video Tutorial May 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Version A: The stack should be efficient when pushing an item.

push:
enqueue in queue1
pop:
while size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2
dequeue and return the last item of queue1, then switch the names of queue1 and queue2

Version B:The stack should be efficient when popping an item

push:
enqueue in queue2
enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2
pop:
deqeue from queue1

- nikhil.kathare September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using c

- pradeep October 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main(){
stack<int>s;queue<int>q;
q.push(20);q.push(30);
for(int i=0;i<q.size();i++){
s.push(q.front());}s.top()=q.back();
cout<<"\n"<<s.top();}

- Anonymous October 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main(){
stack<int>s;queue<int>q;
q.push(20);q.push(30);
for(int i=0;i<q.size();i++){
s.push(q.front());}s.top()=q.back();
cout<<"\n"<<s.top();}

- Anonymous October 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Plz guys help me to develop this in c language

- Yuvaraj June 19, 2018 | 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