ByteByteGo logo
menuProblems List

Implement a Queue using Stacks

Medium

Implement a queue using the stack data structure. Include the following functions:

  • enqueue(x: int) -> None: adds x to the end of the queue.
  • dequeue() -> int: removes and returns the element from the front of the queue.
  • peek() -> int: returns the front element of the queue.

You may not use any other data structures to implement the queue.

Example

Input: [enqueue(1), enqueue(2), dequeue(), enqueue(3), peek()]
Output: [1, 2]

Constraints:

  • The dequeue and peek operations will only be called on a non-empty queue.

You can practice coding exercises online by logging into bytebytego.com on your laptop.