-
What is the minimum number of stacks of size n required to implement a queue of size n ?
-
- one
- Two
- Three
- Four
- one
Correct Option: B
2 stacks. You can even simulate a queue using only one stack. The second (temporary) stack can be simulated by the call stack of recursive calls to the insert method. The principle stays the same when inserting a new element into the queue:
⚈ You need to transfer elements from one stack to another temporary stack, to reverse their order. Then push the new element to be inserted, onto the temporary stack. Then transfer the elements back to the original stack.
⚈ The new element will be on the bottom of the stack, and the oldest element is on top (first to be popped).