Last Updated: March 29, 2026
We need to simulate a stack (LIFO) using only queues (FIFO). The fundamental challenge is that these two data structures have opposite ordering. A stack gives you the most recently added element first, while a queue gives you the oldest element first.
So the core question is: how do we rearrange elements so that a queue can serve them in stack order? There are a few ways to think about this, and each one shifts the "cost" to a different operation. We can either pay the price when pushing (rearrange immediately so the front of the queue is always the newest element), or pay the price when popping (dig through the queue to find the newest element at retrieval time). There is also a clever single-queue approach that handles everything with just one queue.
1 <= x <= 9 → Values are tiny single-digit integers. No overflow concerns.100 calls → Performance is a non-issue. Even O(n) per operation is fine for n up to 100.pop and top calls are valid → No need to handle empty stack errors.The most natural first attempt is to figure out how to keep the queue in "stack order" at all times. If the front of the queue always holds the most recently pushed element, then pop and top become trivial -- just read from the front.
To achieve this, we use two queues. When we push a new element, we put it into the empty secondary queue first, and then move everything from the main queue into the secondary queue, one by one. This places the new element at the front, with all older elements lined up behind it in reverse order. Then we swap the two queues so the secondary becomes the main.
The trade-off is clear: every push costs O(n) because we move all existing elements, but pop and top are O(1) since the answer is always sitting at the front.
q1 (main) and q2 (helper).x into q2. Then dequeue every element from q1 and enqueue it into q2. Now q2 has elements in stack order with x at the front. Swap q1 and q2.q1 and return it.q1 and return it.q1 is empty.This approach makes push expensive and pop cheap. What if we flipped the trade-off and delayed the rearranging until someone actually asks for the top element?
Instead of paying the cost upfront during push, we can flip the trade-off. Push is dead simple: just enqueue the element into q1. We don't rearrange anything.
The work happens during pop. To find the most recently pushed element (which is at the back of the queue), we dequeue all elements except the last one and move them into q2. The last element remaining in q1 is the one we want to return. Then we swap q1 and q2 so the remaining elements are ready for next time.
This approach is the mirror image of Approach 1. It is better when pushes are far more frequent than pops.
Elements enter q1 in push order: oldest at the front, newest at the back. When we pop, we peel away everything except the last element (the newest one), which is exactly what a stack would return. The elements we moved to q2 are still in their original relative order, ready for the next pop.
The topElement variable is a nice trick to avoid the O(n) cost on top. We update it during push (trivially) and during pop (the second-to-last element moved to q2 becomes the new top).
q1 (main) and q2 (helper). Also track the topElement for O(1) top queries.x into q1. Update topElement = x.q1 except the last one, moving them to q2. Track the second-to-last element as the new topElement. Dequeue the final element from q1 (this is the stack's top). Swap q1 and q2. Return the final element.topElement.q1 is empty.Both approaches so far use two queues. The follow-up asks if we can do it with just one. Can we eliminate the second queue entirely by reusing the same queue with a rotation trick?
Here is the clever insight: we don't actually need a second queue. After pushing a new element to the back of a single queue, we can rotate the entire queue by dequeuing elements from the front and re-enqueuing them at the back. If we do this exactly size - 1 times, the new element ends up at the front.
Think of it like a circular conveyor belt. You put a new item at the end, then rotate the belt until that item reaches the front. All the older items maintain their relative order behind it.
This is the most elegant solution and the one interviewers usually hope to see for the follow-up.
After pushing element x and rotating size - 1 times, every element that was in front of x has been moved behind it. The queue now has x at the front, followed by all previous elements in reverse insertion order, which is exactly stack order. Since we maintain this invariant after every push, pop and top always find the correct element at the front.
q.x at the back of q. Then rotate: dequeue from the front and enqueue to the back, repeated size - 1 times. Now x is at the front.q and return it.q and return it.q is empty.