 All Problems
Implement Queue using Stacks
easy
stack
queue
design
amazon
microsoft

Implement a first-in first-out (FIFO) queue using only two stacks. The implemented queue should support all standard queue functions: push, pop, peek, and empty.

  • push(x) — pushes element x to the back of the queue.
  • pop() — removes and returns the element from the front of the queue.
  • peek() — returns the element at the front of the queue.
  • empty() — returns true if the queue is empty, false otherwise.

Example:

Input:
push 1
push 2
peek
pop
empty

Output:
1
1
false

Constraints:

  • 1 ≤ x ≤ 9
  • At most 100 calls total.
  • pop and peek are always called on a non-empty queue.

Input format: One command per line. push X enqueues X. pop prints and removes front. peek prints front. empty prints true or false.

Output format: Print a result line for pop, peek, and empty.

Run to check your code against the sample cases, or submit to run every case