DNAnexus interview question

Interview 1: Using 2 stacks (with only pop and push operations) implement a queue.

Interview Answer

Anonymous

4 Jan 2016

I coded this in Python and came up with an insertion - O(1) and deletion O(n) efficiency. For insertion - I inserted into one stack on the top For deletion - I popped all elements onto stack 2 and pushed a new element and popped them back to stack 1 The expected solution was O(1) for both. The trick is using 2 stacks the right way. It was a pretty clever question! Do try this out as its quite simple.