Skip to main content
Back to problems
#622
Medium Algorithms

Design circular queue

Array Linked List Design Queue
54.0% acceptance
Feb 20, 2026
3830
348
Design your implementation of the circular queue using an array.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
struct MyCircularQueue {
  data: Vec<i32>,
  head: usize,
  tail: usize,
  size: usize,
  capacity: usize,
}

impl MyCircularQueue {
  fn new(k: i32) -> Self {
    MyCircularQueue {
      data: vec![0; k as usize],
      head: 0,
      tail: 0,
      size: 0,
      capacity: k as usize,
    }
  }

  fn en_queue(&mut self, value: i32) -> bool {
    if self.is_full() {
      return false;
    }
    self.data[self.tail] = value;
    self.tail = (self.tail + 1) % self.capacity;
    self.size += 1;
    true
  }

  fn de_queue(&mut self) -> bool {
    if self.is_empty() {
      return false;
    }
    self.head = (self.head + 1) % self.capacity;
    self.size -= 1;
    true
  }

  fn front(&self) -> i32 {
    if self.is_empty() { -1 } else { self.data[self.head] }
  }

  fn rear(&self) -> i32 {
    if self.is_empty() {
      -1
    } else {
      self.data[(self.tail + self.capacity - 1) % self.capacity]
    }
  }

  fn is_empty(&self) -> bool {
    self.size == 0
  }

  fn is_full(&self) -> bool {
    self.size == self.capacity
  }
}