Microsoft Interview Question for SDE1s


Country: United States




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

class Stack<T>{
	private Queue<T> q1 = new LinkedList();
	private Queue<T> q2 = new LinkedList();

	public void push(T elem){
		q1.offer(elem);
	}

	public T pop(){
		while(q1.size() > 1) 	{
			q2.offer(q1.poll());
		}	

		T result = q1.poll();

		while(!q2.isEmpty()) {
			q1.offer(q2.poll);
		}

		return result;
	}
}

- glebstepanov1992 January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it necessary to use 2 queue?
can't it be done using only one queue?

public T pop(){
size = q1.size()
while(size > 1) {
q1.offer(q1.poll());
size--;
}

T result = q1.poll();

return result;
}

- jigs January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Queue is an interface,so you cannot create object. You need to do it as
Queue<Integer> q=new LinkedList<Integer>():..or someother data structure.

- xyz January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

your queue can be of Integer.Max in Java because when you look at the java code of Queue you will find that capacity is an int.

- TTB January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class QueueStack<T> {
	Queue<T> queue1 = new ConcurrentLinkedQueue<T>();
	Queue<T> queue2 = new ConcurrentLinkedQueue<T>();
	
	public void push(T elem){
		queue1.offer(elem);
	}
	
	public T pop(){
		if(!queue1.isEmpty()){
			while(queue1.size()>1){
				queue2.offer(queue1.poll());
			}
			return queue1.poll();
		}else if(!queue2.isEmpty()){
			while(queue2.size()>1){
				queue1.offer(queue2.poll());
			}
			return queue2.poll();
		}else{
			return null;
		}
	}
	
	public T peek(){
		if(!queue1.isEmpty()){
			while(queue1.size()>1){
				queue2.offer(queue1.poll());
			}
			return queue1.peek();
		}else if(!queue2.isEmpty()){
			while(queue2.size()>1){
				queue1.offer(queue2.poll());
			}
			T desiredelem = queue2.poll();
			queue1.offer(desiredelem);
			return desiredelem;
		}else{
			return null;
		}
	}
	
	public static void main(String[] args){
		QueueStack<Integer> testStack = new QueueStack<Integer>();
		testStack.push(1);
		testStack.push(2);
		testStack.push(3);
		System.out.println(testStack.pop());
		System.out.println(testStack.pop());
		System.out.println(testStack.pop());
	}

}

- tian February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think 32768.. ( Max size until stack overflow occurs(65536) /2 )

- Vatsal February 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Infinite number of elements but you can choose to implement the Queue yourself and place a restriction on the max number of elements you wanna be able to add.
So you had a Google and Microsoft interview on the same day!
Either a weird coincidence or some simple homework.

- teli.vaibhav January 17, 2014 | 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