Tap here to switch tabs
Problem
Ask
Submissions

Problem: Implement Queue Using Stacks

easy
15 min
Understand how to design and implement a queue using two stacks by applying stack operations such as push, pop, peek, and empty. Explore techniques to solve this common interview problem and practice coding it in a hands-on environment.

Statement

Design a custom queue, MyQueue, using only two stacks. Implement the Push(), Pop(), Peek(), and Empty() methods:

  • Void Push(int x): Pushes element at the end of the queue.
  • Int Pop(): Removes and returns the element from the front of the queue.
  • Int Peek(): Returns the element at the front of the queue.
  • Boolean Empty(): Returns TRUE if the queue is empty. Otherwise FALSE.

You are required to only use the standard stack operations, which means that only the Push() to top, Peek() and Pop() from the top, Size(), and Is Empty() operations are valid.

Note: In some interview questions, Void Push(int x) and Int Pop() might be referred to as Void Enqueue(int x) and Int Dequeue(), respectively.

Constraints:

  • 1<=1 <= x <=100<= 100
  • A maximum of 100100 calls can be made to Push(), Pop(), Peek(), and Empty().
  • The Pop() and Peek() methods will always be called on non-empty stacks.
Tap here to switch tabs
Problem
Ask
Submissions

Problem: Implement Queue Using Stacks

easy
15 min
Understand how to design and implement a queue using two stacks by applying stack operations such as push, pop, peek, and empty. Explore techniques to solve this common interview problem and practice coding it in a hands-on environment.

Statement

Design a custom queue, MyQueue, using only two stacks. Implement the Push(), Pop(), Peek(), and Empty() methods:

  • Void Push(int x): Pushes element at the end of the queue.
  • Int Pop(): Removes and returns the element from the front of the queue.
  • Int Peek(): Returns the element at the front of the queue.
  • Boolean Empty(): Returns TRUE if the queue is empty. Otherwise FALSE.

You are required to only use the standard stack operations, which means that only the Push() to top, Peek() and Pop() from the top, Size(), and Is Empty() operations are valid.

Note: In some interview questions, Void Push(int x) and Int Pop() might be referred to as Void Enqueue(int x) and Int Dequeue(), respectively.

Constraints:

  • 1<=1 <= x <=100<= 100
  • A maximum of 100100 calls can be made to Push(), Pop(), Peek(), and Empty().
  • The Pop() and Peek() methods will always be called on non-empty stacks.