Macquarie Group Interview Question

Implement a Queue with two stacks

Interview Answer

Anonymous

Aug 11, 2017

Take two empty stacks and start pushing on first stack until it overflows, then from the stack 1 pop one by one element and pushed into stack two till the empty of stack 1 after that return the stack 2. Ex. input --> 1,2,3,4,5; stack 1= 1,2,3,4,5;[LIFO] ; with respect to input. stack 2 = empty; push it to stack 2 one by one : stack 2 = 5,4,3,2,1 ;[LIFO] with respect to stack 1. now stack 1 is empty; return stack 2: 5,4,3,2,1 [FIFO] with respect to input.

1