Amazon interview question

Using Stack structure only to implement Queue. How to enqueue, dequeue?

Interview Answer

Anonymous

7 Sept 2011

Have 2 stacks, called IN and OUT. The queue operation becomes IN.push(e). For the dequeue operation, if OUT is not empty, just return OUT.pop(). If OUT is empty, pop everything from IN and push it into OUT, then return OUT.pop().

4