Microsoft Interview Question for Software Engineer / Developers






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

No need to push elements on to the second stack whenever a enqueue is requested. Just keep adding to first stack. When a dequeue is requested check if there are elements on stack2, if stack2 is empty keep popping from the first stack and placing them onto second stack until the requested number of pops.

Only thing need to remember is always dequeue happens from stack2 and enqueue happens in stack1.

- SS June 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there are no constraints on the memory usage, I would use 2 stacks -

1) For enqueue -
if(num_of_elem <=1)
push_stack1(element)
else
push_stack1(element)
while(top)
push_stack2(pop_stack1())

2) Dequeue -
while(element)
pop_stack2()

Here, the basic logic is push the elements in stack1 and keep a reverse order in stack2. This way you can perform the queue operations using the combination of push/pop from stack1 and stack2.

- O(1) February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

when u enque on stack1 how do u add that element at the bottom of stack2? or u just empty stack 2 and do the process from start?

- sidd.scope July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As SS said, this worked fantastically for me:

1  public class MyQueue<T> {
2    Stack<T> s1, s2;
3    public MyQueue() {
4     s1 = new Stack<T>();
5     s2 = new Stack<T>();
6    }
7   
8    public int size() { 
9     return s1.size() + s2.size(); 
10    }
11   
12    public void add(T value) { 
13     s1.push(value); 
14    }
15   
16    public T peek() {
17     if (!s2.empty()) return s2.peek();
18     while (!s1.empty()) s2.push(s1.pop());
19     return s2.peek(); 
20    }
21   
22    public T remove() {
23     if (!s2.empty()) return s2.pop();
24     while (!s1.empty()) s2.push(s1.pop());
25     return s2.pop();
26    }
27  }

- we_are_anon June 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please next time can you put your source ?
It is the exact copy of Cracking the Coding Interview

- anon October 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

nice solution.

- vhajela February 04, 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