↓Skip to main content
  1. LeetCode/

LeetCode 232: Implement Queue using Stacks

·1 min· ·
LeetCode Easy Stack Design Queue
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-22

  • Submissions: Accepted (first try)

Approach
#

inbox takes pushes and outbox serves pop and peek (Queue and Design). When outbox is empty, pop everything from inbox into it, reversing the order so the oldest element is on top. Each element moves once, so operations are amortized O(1). Never refill a non-empty outbox, or the order breaks.

My Attempts
#

Passed on the first try, but using a list with pop(0) as the queue, not the two stacks the problem asks for, and pop(0) is O(n).

Solution
#

class MyQueue:
    def __init__(self):
        self.inbox, self.outbox = [], []

    def push(self, x: int) -> None:
        self.inbox.append(x)

    def _move(self):
        if not self.outbox:
            while self.inbox:
                self.outbox.append(self.inbox.pop())

    def pop(self) -> int:
        self._move()
        return self.outbox.pop()

    def peek(self) -> int:
        self._move()
        return self.outbox[-1]

    def empty(self) -> bool:
        return not self.inbox and not self.outbox

Complexity
#

Amortized O(1), space O(n).

Mistakes to Avoid
#

  • Refill only when outbox is empty.
  • empty requires both stacks to be empty.

Interview Explanation
#

With two stacks, pushes go to the first and pops and peeks come from the second, which is refilled from the first only when empty, reversing the order into FIFO. Each element moves at most once, so operations are amortized O(1).

Related

LeetCode 225: Implement Stack using Queues
·2 mins
LeetCode Easy Stack Design Queue
LeetCode 225 note: one queue suffices: after a push, rotate all earlier elements to the back so the new element sits at the front
LeetCode 234: Palindrome Linked List
·2 mins
LeetCode Easy Linked-List Two-Pointers Stack Recursion
LeetCode 234 note: find the middle with fast and slow pointers, reverse the second half, and compare from both ends with O(1) extra space
LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop
LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first
LeetCode 94: Binary Tree Inorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 94 note: iterative in-order traversal, pushing nodes while going left, recording on pop, then turning to the right subtree
LeetCode 231: Power of Two
·2 mins
LeetCode Easy Math Bit-Manipulation Recursion
LeetCode 231 note: a power of two has exactly one set bit, and n & (n - 1) clears the lowest one, so the result is 0 exactly then; n must be positive