employer cover photo
employer logo

VMware Interview Question

Implement a queue using stack.

Interview Answers

Anonymous

Jun 23, 2015

Don't you need to call flipStacks() again after s2.pop()?

Anonymous

May 23, 2015

public class QueueWithSTack { private Stack s1 = new Stack(); private Stack s2 = new Stack(); public synchronized void add(T item) { s1.push(item); } public synchronized T remove() { flipStacks(); return s2.pop(); } private void flipStacks() { while (!s1.isEmpty()) { T item = s1.pop(); s2.push(item); } } }