Skip to main content
Back to problems
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 element x onto the stack
  • pop(): remove and return the top element
  • top(): return the top element without removing it
  • empty(): 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() and top() 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(), and empty() take no arguments.

Output Format

  • pop() returns the removed top element.
  • top() returns the current top element.
  • empty() returns true if the stack has no elements, otherwise false.

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
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.