k = 3
enQueue(1), enQueue(2), enQueue(3), enQueue(4),
deQueue(), enQueue(4), Front(), Rear()[True, True, True, False, True, True, 2, 4]Implement MyCircularQueue(k) with capacity k and first-in, first-out order. enQueue(value) inserts and returns True, or False when full. deQueue() removes the oldest value and returns True, or False when empty. Front() and Rear() read without removing and return -1 when empty. isEmpty() and isFull() report the state. Do not use a built-in queue.
k = 3
enQueue(1), enQueue(2), enQueue(3), enQueue(4),
deQueue(), enQueue(4), Front(), Rear()[True, True, True, False, True, True, 2, 4]The fourth insertion fails because the queue is full. Removing 1 makes room for 4. The logical order becomes [2, 3, 4], so Front returns 2 and Rear returns 4.
k = 1
deQueue(), Front(), enQueue(0), isFull(), Rear()[False, -1, True, True, 0]An empty queue cannot remove or read a value. Zero is a valid value: use size, not slot contents, to detect emptiness.
Capacity is from 1 to 1000, inserted values from 0 to 1000, and there are at most 3000 method calls. The animation allows up to 8 slots and 30 operations for readability.
LeetCode 622 ↗k = 3 enQueue(1), enQueue(2), enQueue(3), enQueue(4), deQueue(), enQueue(4), Front(), Rear() [True, True, True, False, True, True, 2, 4]
Initialization takes O(k). deQueue shifts up to k - 1 values, so it takes O(k) in the worst case. All other methods take O(1).
The array contains k slots. The counters use O(1) additional space. Here k is the queue capacity.
k = 3 enQueue(1), enQueue(2), enQueue(3), enQueue(4), deQueue(), enQueue(4), Front(), Rear() [True, True, True, False, True, True, 2, 4]
Moving an index replaces all the shifts. After wraparound, physical array order can differ from queue order; follow front for exactly size values.
Initialization takes O(k). Every queue method takes O(1) worst-case time: it changes only a few indices or values, without shifting.
The array contains k slots. The counters use O(1) additional space. Here k is the queue capacity.