Implement a bounded first-in-first-out queue
Quick answer
A queue processes the earliest enqueued item first (FIFO). A circular buffer stores a bounded queue without shifting every remaining item.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const q=["A","B","C"];let head=0;console.log(q[head++],q[head++],q[head],q.length-head);Expected console output:
A B C 1The head advances while the next item remains C.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const capacity=3,slots=new Array(capacity);let head=0,size=0;function add(x){if(size===capacity)throw Error("full");slots[(head+size)%capacity]=x;size++;}function take(){if(size===0)throw Error("empty");const x=slots[head];slots[head]=undefined;head=(head+1)%capacity;size--;return x;}add("A");add("B");add("C");const first=take();add("D");console.log(first,take(),take(),take(),size);Expected console output:
A B C D 0Insertion wraps to the freed slot while FIFO order is maintained.
How it works and cost model
For a positive capacity c, keep head and size. The next insertion index is (head + size) % c. On removal, read head, clear its slot to release the reference, advance head = (head+1)%c and decrement size. Size distinguishes empty and full even if the head wraps. With fixed-size array slots and bounded arithmetic, each enqueue/dequeue uses O(1) operations; storage O(c). Decide whether full/empty throws or returns a tagged result. An array with a growing head index is also useful for bounded one-shot traversal, but retained consumed elements should not accumulate in long-lived queues.
Common mistakes
Never overwrite a full queue unless overwrite is explicitly the policy. Do not rely on a fixed complexity guarantee for JavaScript shift().
Practice
Simulate a capacity-three circular queue: add A,B,C, remove A, add D, then drain. Check wrap-around and full/empty handling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Implement a bounded first-in-first-out queue — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What distinguishes a full circular queue from an empty one in the head-and-size design?
What index receives the next item in a nonfull circular buffer of capacity c?
Why clear a consumed array slot in a long-lived queue?
What is the exact console output in the FIFO circular queue example 1?
const q=["A","B","C"];let head=0;console.log(q[head++],q[head++],q[head],q.length-head);What is the exact console output in the FIFO circular queue example 2?
const capacity=3,slots=new Array(capacity);let head=0,size=0;function add(x){if(size===capacity)throw Error("full");slots[(head+size)%capacity]=x;size++;}function take(){if(size===0)throw Error("empty");const x=slots[head];slots[head]=undefined;head=(head+1)%capacity;size--;return x;}add("A");add("B");add("C");const first=take();add("D");console.log(first,take(),take(),take(),size);