Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #641: Design Circular Deque

In this guide, we solve Leetcode #641 Design Circular Deque in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Design your implementation of the circular double-ended queue (deque). Implement the MyCircularDeque class: MyCircularDeque(int k) Initializes the deque with a maximum size of k.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Design, Queue, Array, Linked List

Intuition

We need level-order exploration or shortest-step expansion, which maps directly to a queue.

BFS guarantees the first time you reach a node is the shortest in unweighted graphs.

Approach

Initialize the queue with starting nodes and expand outward layer by layer.

Track visited nodes to avoid cycles and redundant work.

Steps:

  • Initialize a queue with start nodes.
  • Pop, process, and enqueue neighbors.
  • Track visited nodes.

Example

Input ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] [[3], [1], [2], [3], [4], [], [], [], [4], []] Output [null, true, true, true, false, 2, true, true, true, 4] Explanation MyCircularDeque myCircularDeque = new MyCircularDeque(3); myCircularDeque.insertLast(1); // return True myCircularDeque.insertLast(2); // return True myCircularDeque.insertFront(3); // return True myCircularDeque.insertFront(4); // return False, the queue is full. myCircularDeque.getRear(); // return 2 myCircularDeque.isFull(); // return True myCircularDeque.deleteLast(); // return True myCircularDeque.insertFront(4); // return True myCircularDeque.getFront(); // return 4

Python Solution

class MyCircularDeque: def __init__(self, k: int): self.k = k + 1 self.data = [0] * self.k self.front = 0 self.rear = 0 def insertFront(self, value: int) -> bool: if self.isFull(): return False self.front = (self.front - 1) % self.k self.data[self.front] = value return True def insertLast(self, value: int) -> bool: if self.isFull(): return False self.data[self.rear] = value self.rear = (self.rear + 1) % self.k return True def deleteFront(self) -> bool: if self.isEmpty(): return False self.front = (self.front + 1) % self.k return True def deleteLast(self) -> bool: if self.isEmpty(): return False self.rear = (self.rear - 1) % self.k return True def getFront(self) -> int: return -1 if self.isEmpty() else self.data[self.front] def getRear(self) -> int: return -1 if self.isEmpty() else self.data[(self.rear - 1) % self.k] def isEmpty(self) -> bool: return self.front == self.rear def isFull(self) -> bool: return (self.rear + 1) % self.k == self.front

Complexity

The time complexity is O(V+E). The space complexity is O(V).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy