Hints — p98 Design Circular Queue
-
State =
arr: List[int]of size k +front: int+size: int. Avoid storingreardirectly — derive it. -
Next write index =
(front + size) % k. Rear index =(front + size - 1) % k. -
Full vs empty disambiguation: with a
sizecounter,size == 0means empty andsize == kmeans full. No pointer-comparison ambiguity. -
Front()/Rear()on empty must return -1 (per LC spec); don’t index intoarr. -
Real-world: this is a ring buffer. Production extensions: lock-free via atomic head/tail (SPSC: easy; MPMC: hard — see LMAX Disruptor); cache-line padding to avoid false sharing.
If stuck: see solution.py.