Very common white board question.


Assuming we have two stacks Stack_In and Stack_Out.

1. Then an element is being inserted in the queue we push it into Stack_In.

2. Then an element is dequeued, if Stack_Out is not empty we pop it from the Stack_Out, else if Stack_Out is empty then all elements currently in Stack_In are transferred to Stack_Out in the reverse order.

For coding solution in Java follow original link.

Answers and Comments