Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Queue can be implemented using two stacks, but either Enqueue or Dequeue is expensive

Steps:
1. Create stacks s1 and s2
2. Enqueue operation - push data into s1 then pop all items from s1 and push it into s2
3. Dequeue operation - pop from s2

- siva December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Step 2 - you can't simply pop all items from s1 if s2 is not empty. Imagine this:

Before:      After
S1  S2       S1  S2 
7   1            5
6   2            6
5   3            7
    4            1 
                 2
                 3
                 4

That can be fixed, of course (for example, by remembering which of stacks is "main" by the moment - depending on the last operation and storing all data in that stack, etc.). Just wanted to point out that your solution is not complete.

- littorio December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone also tell me how to implement a stack using two queues?

- alex December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Add one step more to step 2, if s2 non empty, pop and push all to s1, then push new value

- frank December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;


public class QueueByStacks {
	Stack<Integer> master;
	Stack<Integer> slave;
	
	
	public QueueByStacks() {
		super();
		master = new Stack<Integer>();
		slave = new Stack<Integer>();
	}

	public void enque(int element){
		master.add(element);
	}
	
	public Integer deque(){
		if(slave.isEmpty()){
			while(!master.isEmpty()){
				slave.add(master.pop());
			}
		}else{
			return slave.pop();
		}
		if(slave.isEmpty()){
			return null;
		}else{
			return slave.pop();
		}
	}
	
	public static void main(String[] args) {
		QueueByStacks queue = new QueueByStacks();
		queue.enque(3);
		queue.enque(4);
		System.out.println(queue.deque());
		queue.enque(5);
		System.out.println(queue.deque());
		System.out.println(queue.deque());
		System.out.println(queue.deque());
	}

}

- Kevin March 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Queue {
public:
	int top();
	int dequeue();
	void enqueue(int v);
	int size();
private:
	bool uprightStackActive;
	stack<int> uprightStack;
	stack<int> reverseStack;
	void copy(stack<int>& from, stack<int>& to) {
		while(from.size() > 0) {
			to.push(from.top());
			from.pop();
		}
	}
};

int Queue::top() {
	if(uprightStackActive) {
		copy(uprightStack, reverseStack);
		uprightStackActive = false;
	} 
	return reverseStack.top();
}
int Queue::dequeue() {
	if(uprightStackActive) {
		copy(uprightStack, reverseStack);
		uprightStackActive = false;
	} 
	auto ret = reverseStack.top();
	reverseStack.pop();
	return ret;
}
void Queue::enqueue(int v) {
	if(!uprightStackActive) {
		copy(reverseStack, uprightStack);
		uprightStackActive = true;
	}
	uprightStack.push(v);
}
int Queue::size() {
	return uprightStack.size() + reverseStack.size();
}

- Mhret November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

test

- test December 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

test

- test December 11, 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