Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0232 - Implement Queue using Stacks
    0232 - Implement Queue using Stacks
    0232 - Implement Queue using Stacks
    0232 - Implement Queue using Stacks

    0232 - Implement Queue using Stacks

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/implement-queue-using-stacks/

    Solution 1 - Two Stack Approach

    Implement a FIFO queue using two stacks. Use one stack (input_stack) for pushing elements and another (output_stack) for popping/peeking. When output_stack is empty and a pop/peek is needed, transfer all elements from input_stack to output_stack, reversing their order to achieve FIFO behavior.

    TimeComplexity:O(1) amortizedTimeComplexity: O(1) \text{ amortized}TimeComplexity:O(1) amortized
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    """ 0232.1 - Solution 1 - Two Stack Approach """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class MyQueue:
        """Queue implemented using two stacks."""
    
        def __init__(self):
            """Initialize two stacks: input for push, output for pop/peek."""
            self.input_stack = []
            self.output_stack = []
    
        def push(self, x: int) -> None:
            """Push element x to the back of the queue."""
            self.input_stack.append(x)
    
        def pop(self) -> int:
            """Remove the element from the front of the queue and return it."""
            self.peek()
            return self.output_stack.pop()
    
        def peek(self) -> int:
            """Return the element at the front of the queue."""
            if not self.output_stack:
                while self.input_stack:
                    self.output_stack.append(self.input_stack.pop())
            return self.output_stack[-1]
    
        def empty(self) -> bool:
            """Return True if the queue is empty, False otherwise."""
            return not self.input_stack and not self.output_stack
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        q = MyQueue()
        q.push(1)
        q.push(2)
        print(q.peek())   # 1
        print(q.pop())    # 1
        print(q.empty())  # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()