Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement MyCircularQueue(k) with enQueue, deQueue, Front, Rear, isEmpty, and isFull. enQueue/deQueue return success booleans; Front/Rear return -1 when empty.

Starter code

class MyCircularQueue:
    def __init__(self, k):
        pass
Test cases

wrap-and-full

{
  "operations": [
    "MyCircularQueue",
    "enQueue",
    "enQueue",
    "enQueue",
    "enQueue",
    "Rear",
    "isFull",
    "deQueue",
    "enQueue",
    "Rear"
  ],
  "arguments": [
    [
      3
    ],
    [
      1
    ],
    [
      2
    ],
    [
      3
    ],
    [
      4
    ],
    [],
    [],
    [],
    [
      4
    ],
    []
  ]
}

Expected: [null,true,true,true,false,3,true,true,true,4]

Wizard outline
  1. Step 1: Initialize MyCircularQueue

    Replace the empty starter with the first real state owned by MyCircularQueue. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Wizard Stateful Circular Fill case

    Complete the readable core algorithm for one representative Interview case. The logical tail is head + count modulo capacity, so physical wrap does not change queue order.

  4. Step 4: Harden the Wizard Stateful Circular Wrap boundary

    Repair the reviewed boundary and pass the complete submission contract. Advancing head modulo capacity and decrementing count preserves both FIFO order and full/empty meaning.

Footguns and prerequisites
  • Rear is at (head+count-1)%capacity, not the next insertion slot.
  • python specific rapid fire
Reviewed references
Practice prerequisites
  • Advance a Circular Buffer(opens in a new tab)

    Advance a Circular Buffer isolates head identifies the oldest live value, tail identifies the next write slot, and size stays between zero and capacity. That focused state discipline is required when implementing design circular queue as a complete Interview Problem.

Recommended approach and implementation

Allocate k slots and track head plus count. Insert at (head+count)%k, remove by advancing head modulo k, and derive rear from head+count-1.

Why it works: The count consecutive modular slots starting at head are exactly the queued elements in FIFO order. Each operation updates head or count while preserving that representation, making empty/full and both endpoints exact.

class MyCircularQueue:
    """
    Checkpoint 1: initialize the state owned by this Interview contract.
    Checkpoint 2: assemble the primary transition without hiding the boundary.
    """
    def __init__(self, k):
        self.values = [0] * k
        self.capacity = k
        self.head = 0
        self.count = 0
    def enQueue(self, value):
        if self.isFull():
            return False
        self.values[(self.head + self.count) % self.capacity] = value
        self.count += 1
        return True
    def deQueue(self):
        if self.isEmpty():
            return False
        self.head = (self.head + 1) % self.capacity
        self.count -= 1
        return True
    def Front(self):
        return -1 if self.isEmpty() else self.values[self.head]
    def Rear(self):
        if self.isEmpty():
            return -1
        index = (self.head + self.count - 1) % self.capacity
        return self.values[index]
    def isEmpty(self):
        return self.count == 0
    def isFull(self):
        return self.count == self.capacity