Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods
Implement a FIFO queue using only stack operations (push/pop/peek on arrays). Implement `MyQueue`: - `push(x)` adds to the back. - `pop()` removes and returns the front. - `peek()` returns the front. - `empty()` returns whether the queue is empty.
+ 1 hidden test run on Submit.
Two stacks reverse order twice, restoring FIFO. Each element moves at most once between stacks, giving O(1) amortized operations.
Run your code to see results.