## Microsoft Interview Question for Software Engineer / Developers

• 1
of 1 vote

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

ah sorry! the question was to implement enqueue(int i) and dequeue() using stack ADT

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

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

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

void enqueue(int i)
{
push(i);
}
int dequeue()
{
int temp1,temp2;
if (!count()) return ***;//TBD
if (count()==1) temp2=pop();
else {
temp1=pop();
temp2=dequeue();
enqueue(temp1);
}
return temp2;
}

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

``````#define QUEUE_EMPTY -9999

int dequeue()
{
int temp,result;
if (!count())
return QUEUE_EMPTY ;//Or call pop again its return
//value finally returned
if (count()==1)
{
temp=pop();
return temp;
}

temp=pop();
result=dequeue();
push(temp);

return result;
}``````

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

ginago...
d ko kainchindi...

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

doesnt a queue add from the bottom ,the above solution seems to be opposite of what needs to be done

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

I'm not sure it matters, as long as it's FIFO it's a queue. That's the sort of thing you would want to ask for clarification about though.

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

temp2=dequeue();

should it not just be dequeue();

why do we need to return the value of dequeue every time in temp2?

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

temp2 is used to carry the last element all the way through the recursion. Nice logic !

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

Use two stacks: S and T.

For enqueue(), just invoke S.push()

For dequeue(), pop each item in S and push it into T one by one. And the last item is the one to dequeue. And as the final step, pop each item in T and push it into S one by one.

Time efficiency might be a problem here...

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

Update my algorithm above.

For enqueue(),
1) if S.count() = 0 and T.count() != 0, then pop each item in T and push it into S one by one
2) Invoke S.push() to enqueue the new item

For dequeue(),
1) if S.count() > 0, then pop each item in S and push it into T
2) Invoke T.pop() to dequeue an item

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

anong klaseng java yan...

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

anong klaseng java yan...

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

anong klaseng java yan...

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

anong klaseng java yan...

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

``````enQ{
push(a);
}``````

``````_dQ(data){

if(stack_empty)
return data;

temp=pop();
a=dQ(temp);
push(temp);
return a;
}``````

``````dequeue(){
if(stack_empty)
throw Exception;
return  _dQ(0);
}``````

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

A Java implementation using only one stack. Use recursion to implement dequeue.

``````public class ID64863
{
// Use only one stack. Use recursion to implement dequeue.
StackWithCount stack=new StackWithCount();

public void enqueue(int element)
{
stack.push(element);
}
public int dequeue()
{
if (stack.count()<=1)
return stack.pop();
int top=stack.pop();
int result=dequeue();
stack.push(top);
return result;
}

public static void main(String[] args)
{
ID64863 queue=new ID64863();
queue.enqueue(0);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
}
public class StackWithCount extends Stack<Integer>
{
public int count()
{
return elementCount;
}
}
}``````

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.

### 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.