Microsoft Interview Question for Software Engineer / Developers






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

- zaya November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what does ADT mean?

- anonymous November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ADT-abstract data type

- Anonymous October 05, 2013 | Flag
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;
}

- anonymous November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#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;
}

- Hemant Jain February 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ginago...
d ko kainchindi...

- jorge January 06, 2011 | Flag
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

- Anonymous November 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous November 16, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please comment on the statement
temp2=dequeue();

should it not just be dequeue();

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

- Kshitiz November 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Das December 09, 2008 | Flag
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...

- Jackie Wong December 02, 2008 | Flag Reply
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

- Jackie Wong December 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anong klaseng java yan...

- may-ann January 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anong klaseng java yan...

- may-ann January 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anong klaseng java yan...

- may-ann January 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anong klaseng java yan...

- may-ann January 15, 2011 | Flag Reply
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);
}

- cl4w August 02, 2012 | Flag Reply
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;
		}
	}
}

- wangpidong October 15, 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