#225 Implement Stack using Queues
the problem
In plain words
Build a LIFO stack (push, pop, top, empty) using only standard queue operations: push to back, peek/pop from front, size and isEmpty.
in push(1), push(2), top(), pop(), empty()
out 2, 2, false
- 1 ≤ x ≤ 9
- At most 100 calls
- pop and top are always valid
- Follow-up: use only one queue
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternStack & queue design
A queue can't reverse itself, but it can rotate: poll from the front and offer to the back. Rotate n−1 times after a push and the newest element reaches the front.
solutions
From brute force to optimal
Push is a plain offer. To pop, move all but the last element to a helper queue, take the last one, then swap the queues.
- push:
main.offer(x). - pop: move
size − 1elements tohelper; poll the remaining one; swapmainandhelper. - top: pop, then push the value back.
Each pop moves n − 1 elements.
Elements are split between the two queues.
// On LeetCode, name this class MyStack.class MyStackTwoQueues { private Queue<Integer> main = new ArrayDeque<>(); private Queue<Integer> helper = new ArrayDeque<>(); public void push(int x) { main.offer(x); // O(1): newest goes to the rear } public int pop() { while (main.size() > 1) helper.offer(main.poll()); // move all but the newest int top = main.poll(); // the newest is the only one left Queue<Integer> t = main; // swap roles so main holds the rest again main = helper; helper = t; return top; } public int top() { int x = pop(); // reuse pop... push(x); // ...and put it back as the newest return x; } public boolean empty() { return main.isEmpty(); }}After offering x, rotate the older n − 1 elements behind it so the front is always the top of the stack.
- push: offer x, then
size − 1times doq.offer(q.poll()). - pop:
q.poll(). - top:
q.peek().
Each push rotates n − 1 elements; reading the front is O(1).
One queue of n elements — and no second queue, which answers the follow-up.
class MyStack { private final Queue<Integer> q = new ArrayDeque<>(); // front = top of the stack public void push(int x) { q.offer(x); for (int i = 0; i < q.size() - 1; i++) { q.offer(q.poll()); // rotate older elements behind x, so x reaches the front } } public int pop() { return q.poll(); } public int top() { return q.peek(); } public boolean empty() { return q.isEmpty(); }}in the room
How to say it in the interview
talk track
- Ask which operation should be cheap — you can make push O(1) or pop O(1), not both.
- The one-queue rotation is the cleanest answer; mention the two-queue version as the alternative.
traps
- Rotating
sizetimes instead ofsize − 1— x goes back to the rear.
what they ask next
Follow-ups
Which is better in practice?
Neither — a real stack is ArrayDeque.push/pop. This problem tests whether you understand both orders.
before you submit
Edge cases to test
- One element: push, then top and pop return it, and empty becomes true.
- Pushes between pops: each push rotates only the older elements (size − 1 of them), never itself.
- top twice in a row returns the same value and removes nothing.
practise next
Similar problems
- #232Implement Queue using StacksEasyin Queues
- #155Min StackMediumin Stacks