Back to problems Sign in to unlock
Leetcode
Medium
Stacks
Queues
Design
Implement Stack using Queues
Design a stack that supports standard stack operations using one or more queues.
Acceptance 100%
Problem Statement
Problem
Implement a last-in, first-out (LIFO) stack using only queue operations.
You need to support the usual stack interface:
push(x): add elementxonto the stackpop(): remove and return the top elementtop(): return the top element without removing itempty(): return whether the stack is empty
You may use one or more queues internally, but the exposed behavior must match a normal stack.
Notes
- The order of removal must follow stack semantics, not queue semantics.
- Assume the caller uses the operations in any valid order.
- If the stack is empty,
pop()andtop()behavior is typically defined by the platform; for practice, you can assume they are called only when valid unless specified otherwise.
Input Format
- A sequence of operations on a stack object.
push(x)receives one integer value.pop(),top(), andempty()take no arguments.
Output Format
pop()returns the removed top element.top()returns the current top element.empty()returnstrueif the stack has no elements, otherwisefalse.
Constraints
- Implement the stack using queue operations only.
- Typical practice constraints are small to moderate, but the design should support repeated operations efficiently enough for interview use.
- Element values are integers.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.