Implement MyCircularQueue(k) with enQueue, deQueue, Front, Rear, isEmpty and isFull, using a fixed array of k slots.
Before you code
Store head and size, not head and tail, and compute everything else with the remainder. Then "full" and "empty" are just size == k and size == 0, with no ambiguity.
Hints
enQueue: if full, return False; otherwise write at(head + size) % kand incrementsize.deQueue: if empty, return False; otherwise advancehead = (head + 1) % kand decrementsize.Rearis the item at(head + size - 1) % k.- Test the wrap-around: fill, remove one, add one, and check
Rear.
Production & complexity notes
When solving Design circular queue on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Queues and ring buffers to reinforce the underlying pattern.
What it practices
Head, size and wrap-around, from Queues and ring buffers.