Problem solution · Python

Design Circular Queue

Design Circular Queue: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Design Circular Queue, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 68 lines of Python from the credited upstream file design-circular-queue.py.
  • The implementation visibly relies on work queue.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeDesign Circular Queue · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(1)# Space: O(k) class MyCircularQueue(object):     def __init__(self, k):        """        Initialize your data structure here. Set the size of the queue to be k.        :type k: int        """        self.__start = 0        self.__size = 0        self.__buffer = [0] * k     def enQueue(self, value):        """        Insert an element into the circular queue. Return true if the operation is successful.        :type value: int        :rtype: bool        """        if self.isFull():            return False        self.__buffer[(self.__start+self.__size) % len(self.__buffer)] = value        self.__size += 1        return True     def deQueue(self):        """        Delete an element from the circular queue. Return true if the operation is successful.        :rtype: bool        """        if self.isEmpty():            return False        self.__start = (self.__start+1) % len(self.__buffer)        self.__size -= 1        return True     def Front(self):        """        Get the front item from the queue.        :rtype: int        """        return -1 if self.isEmpty() else self.__buffer[self.__start]     def Rear(self):        """        Get the last item from the queue.        :rtype: int        """        return -1 if self.isEmpty() else self.__buffer[(self.__start+self.__size-1) % len(self.__buffer)]     def isEmpty(self):        """        Checks whether the circular queue is empty or not.        :rtype: bool        """        return self.__size == 0     def isFull(self):        """        Checks whether the circular queue is full or not.        :rtype: bool        """        return self.__size == len(self.__buffer)    

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗