💡
Seb's Whiteboard
  • 👨‍💻Welcome, I'm Sebastien St Vil
  • Extras
    • Gradient Descent
    • How I learned Java
    • Machine Learning by Andrew Ng
  • Projects
    • 📉Backtest Equity Trading with SMA Strategy
    • Wellington GA Lab
    • Stock analysis
    • 📈Time Series Regression-based Trading Strategy
  • Arrays & Strings
    • Best Time to Buy and Sell Stock II
    • Online Stock Span
    • Implement strStr()
    • 2Sum
    • 3Sum
    • 3Sum Closest
    • 4Sum II
    • Set Matrix Zeroes
    • Group Anagrams
    • Longest Substring Without Repeating Characters
    • Remove Duplicates from Sorted Array
    • Move Zeroes
    • Valid Sudoku
    • Rotate Image
    • First Unique Character in a String
    • Design a Circular Queue
    • Longest Common Prefix
  • Binary Tree
  • Second Minimum Node In a Binary Tree (671)
  • Design
  • LRU Cache
  • Min Stack (155)
  • Sorting & Searching
    • Merge Sorted Array (88)
    • First Bad Version
  • Math
    • Power of Three (326)
    • Power of Two (231)
    • Count Prime (204)
    • Roman to Integer (13)
    • Fizz Buzz (412)
    • Count-and-Say
  • Dynamic Programming
    • Pascal's Triangle (118)
  • Linked List
    • Copy List with Random Pointer
    • Remove Nth Node From End of List
    • Remove Duplicated from Sorted List II
  • Tips
    • Finding dups
  • Sliding Window
    • Subarray Product Less Than K
Powered by GitBook
On this page
  • Approach1: Array
  • Approach2: Linked List

Was this helpful?

  1. Arrays & Strings

Design a Circular Queue

PreviousFirst Unique Character in a StringNextLongest Common Prefix

Last updated 3 years ago

Was this helpful?

Approach1: Array

Based on the description of the problem, an intuitive data structure that meets all the requirements would be a ring where the head and the tail are adjacent to each other.

Since there does not exist such data structure, we will use an array to simulate such behavior. I've also inserted a pictorial representation in the below.

Visual:

As seen in the below example, the below circular Queue is of a maximum capacity of 4.

Since we follow the FIFO properties in a Queue, Index 0 is empty due to a Deque operation being called.

The maximum capacity is 4 however it's current size is 3. In order to take advantage of the empty space, we need to access the first index.

Since we insert at the rear in a Queue, we use the below formula to access the 0th index.

Enque: (Rear + 1) % capacity

Since we dequeue from the front in a Queue

Deque: (Front + 1) % capacity

class MyCircularQueue:

     def __init__(self, k: int):
        self.capacity = k 
        self.size = 0
        self.t = [0] * self.capacity 
        self.front, self.rear = 0, -1

     def enQueue(self, value: int) -> bool:
        if self.isFull(): return False
        
        self.rear = (self.rear + 1) % self.capacity 
        self.t[self.rear] = value
        self.size += 1
        return True

     def deQueue(self) -> bool:
        if self.isEmpty(): return False
        
        self.front = (self.front + 1) % self.capacity
        self.size -= 1
        return True

     def Front(self) -> int:
        if self.isEmpty(): return -1
        
        return self.t[self.front]

     def Rear(self) -> int:
        if self.isEmpty(): return -1
        
        return self.t[self.rear]

     def isEmpty(self) -> bool:

        return self.size == 0

     def isFull(self) -> bool:
        return self.size == self.capacity 

Approach2: Linked List

class ListNode:

    def __init__(self, value=-1, next=None):
        self.value =value
        self.next = next

class MyCircularQueue:

     def __init__(self, k: int):
        self.head = self.tail = None
        self.size = 0
        self.k = k

     def enQueue(self, value: int) -> bool:

        if self.isFull(): return False
        
        node = ListNode(value)
        if self.size == 0:
            self.head = self.tail = node
        else:
            self.tail.next = node
            self.tail = node
            
        self.size += 1
        return True

     def deQueue(self) -> bool:

        if self.isEmpty(): return False
        
        self.head = self.head.next
        self.size -= 1
        return True
        
     def Front(self) -> int:
        return -1 if self.isEmpty() else self.head.value

     def Rear(self) -> int:
        return -1 if self.isEmpty() else self.tail.value

     def isEmpty(self) -> bool:
        return self.size == 0

     def isFull(self) -> bool:
        return self.size == self.k