17 · Queues & Deques

#232 Implement Queue using Stacks

Easysolve on LeetCode ↗

frequently reported atAppleMicrosoftBloombergAmazonMeta

the problem

In plain words

Build a FIFO queue (push, pop, peek, empty) using only standard stack operations: push to top, peek/pop from top, size and isEmpty.

in push(1), push(2), peek(), pop(), empty()

out 1, 1, false

1 went in first, so it comes out first.

  • 1 ≤ x ≤ 9
  • At most 100 calls
  • pop and peek are always valid
  • Follow-up: amortized O(1) per operation

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.

    practise next

    Similar problems

    1. #225
    2. #155
      Min Stack
      Mediumin Stacks
    3. #622
      Design Circular Queue
      Mediumin Queues