Bloomberg interview question

Implement a queue data structure using two stacks

Interview Answer

Anonymous

20 Sept 2010

Queue - First In Last Out Insert the elements into the stack one by one. For removing, use another stack. Pop all the elements from the original stack and insert in the second stack. Now if we pop the element from the second stack, that will be in FIFO order. Basically -(-1) = 1, so Stack(stack) = Queue