Queues: First In, First Out
Serve the earliest waiting student
Maya arrives at a school help desk before Ravi. Unless the school has an explicit priority policy, Maya should be served first. Removing the newest arrival would implement the wrong order.
A queue inserts at the back and removes from the front. Its rule is first in, first out, or FIFO. Enqueue adds and dequeue removes the next item.
Trace arrivals
Predict service order after adding students.
Compare implementations
Explain a small array queue and a head-index approach.
State limits
Handle empty queues without promising durable service or distributed fairness.
Arrival order becomes service order
Arrivals
Add Maya then Ravi.
Front: Maya → RaviService
Remove Maya, the oldest waiting arrival.
Ravi remainsNew arrival
Append Asha behind Ravi.
Ravi → Asha
Front and back have different responsibilities
For a small JavaScript example, push adds at the end and shift removes at the beginning. This demonstrates FIFO clearly. pop would remove at the wrong end for this queue.
shift can require repositioning later entries. Larger queues often use a head index, a ring buffer or a specialized implementation. Correct behavior comes before optimizing the container.
Before starting, review ordered arrays. These examples use JavaScript; other languages can use different syntax and rules.
Serve two students in arrival order
Maya is enqueued first and removed first. Asha arrives after Maya is served, so she waits behind Ravi.
The outputs show Maya, Ravi and Asha. The local array does not contact a help desk or guarantee an actual service action completes.
JavaScript · local teaching example
const waiting = [];
waiting.push('Maya');
waiting.push('Ravi');
console.log(waiting.shift());
waiting.push('Asha');
console.log(waiting.shift());
console.log(waiting.shift());Expected output
Maya
Ravi
AshaAdvance a head rather than shifting every entry
This teaching model reads the current head and increments it. It avoids shifting entries on each removal, but old entries remain in the backing array.
A production implementation should reclaim references and compact or reuse storage. Never let a head-index model imply bounded memory without that work.
JavaScript · local teaching example
const waiting = ['Maya', 'Ravi'];
let head = 0;
console.log(waiting[head]);
head += 1;
console.log(waiting[head]);
head += 1;
console.log(waiting.length - head);Expected output
Maya
Ravi
0Apply the rule to a small change
Begin with an empty array queue. Check its length before dequeueing and display a useful message.
Worked answer
An empty queue has no student to serve. Handle that state explicitly rather than displaying undefined as though it were a student.
JavaScript · local teaching example
const waiting = [];
if (waiting.length === 0) {
console.log('No students waiting');
} else {
console.log(waiting.shift());
}Expected output
No students waitingRecognize mistakes and limits
A local queue is not a durable messaging service: a process crash can lose its contents. Delivery, acknowledgment, retries and concurrency are separate policies for real job queues.
FIFO describes removal order, not guaranteed completion order when multiple workers run concurrently. Priority rules or cancellations also change the basic waiting policy and should be stated to users.
For the language or tool rules used here, see JavaScript indexed collections.
Check the core idea
Apply the idea to a new situation
Separate removal order from service guarantees
Queues model arrival-first work. Handle empty states, select an implementation suited to size, and state persistence and concurrency policies separately.
Connect this idea with latest-first work and work that finishes later.
Reaching the recap and completing the quizzes are separate milestones.