Advertisement

Implement Queue using Stacks - LeetCode 232 Solution

Implement Queue using Stacks - Complete Solution Guide

Implement Queue using Stacks is LeetCode problem 232, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue ( push , peek , pop , and empty ). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false otherwise. Notes: You must use only standard o

Detailed Explanation

The problem asks you to design a queue data structure using only two stacks. A queue follows the FIFO (First-In, First-Out) principle, meaning the first element added is the first element removed. The challenge lies in simulating this behavior using stacks, which follow the LIFO (Last-In, First-Out) principle. You need to implement four methods: `push(x)` to add an element `x` to the rear of the queue, `pop()` to remove and return the element at the front, `peek()` to return the element at the front without removing it, and `empty()` to check if the queue is empty.

Solution Approach

The solution utilizes two stacks, `stack1` and `stack2`. Elements are pushed onto `stack1`. When `pop()` or `peek()` is called and `stack2` is empty, all elements from `stack1` are transferred to `stack2`. This effectively reverses the order of elements, making the element that was at the bottom of `stack1` (the front of the queue) now at the top of `stack2`. Then, the top element of `stack2` can be easily popped or peeked.

Step-by-Step Algorithm

  1. **push(x):** Push the element `x` onto `stack1`.
  2. **pop():** If `stack2` is empty, transfer all elements from `stack1` to `stack2`. Then, pop and return the top element from `stack2`.
  3. **peek():** If `stack2` is empty, transfer all elements from `stack1` to `stack2`. Then, return the top element of `stack2`.
  4. **empty():** Check if both `stack1` and `stack2` are empty. Return `true` if both are empty, `false` otherwise.

Key Insights

  • Using two stacks allows us to efficiently simulate queue behavior. One stack (`stack1`) is used primarily for pushing elements, and the other (`stack2`) is used for popping elements.
  • The key to achieving amortized O(1) time complexity is to only transfer elements between stacks when necessary (i.e., when `pop()` or `peek()` is called and `stack2` is empty).
  • Understanding that the cost of transferring elements between stacks is distributed over multiple operations is crucial for understanding the amortized complexity.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Stack, Design, Queue.

Companies

Asked at: Infosys, Netflix, Oracle, Qualcomm.