Problem Description
Implement a first in first out (FIFO) queue using only two stacks. The queue should support push, pop, peek, and empty operations while only using standard stack operations such as push, pop, peek, and checking if empty. The implementation should also achieve an amortized O(1) time complexity for each operation.
Key Insights
- Use two stacks:
- One (input stack) for enqueue operations.
- The other (output stack) for dequeue operations.
- When performing pop or peek, if the output stack is empty, transfer all elements from the input stack to the output stack. This reversal makes the oldest element accessible.
- Each element is moved at most once between the stacks, ensuring an amortized O(1) cost per operation.
- The space complexity is O(n) where n is the number of elements in the queue.
Space and Time Complexity
Time Complexity: Amortized O(1) per operation Space Complexity: O(n)
Solution
We maintain two stacks: an input stack for pushing new elements and an output stack for popping or peeking the front element of the queue. When we need to access the front of the queue (either during a pop or peek), we check if the output stack is empty. If it is, we transfer all elements from the input stack to the output stack. This reversal of order allows us to simulate the FIFO nature of the queue. When performing a push, the element is simply added to the input stack.