Loading...
Queues organise waiting work. State which item leaves next and how storage is managed before updating front and rear indices.
What you'll be able to do
A normal queue removes items in first-in, first-out order. Enqueue adds at the rear; dequeue removes from the front. A bounded implementation must detect full storage before adding and an empty queue before removing. The item left in an old array slot after removal is not necessarily still logically present.
A simple linear array queue can run out of usable positions at its rear even when earlier positions were freed. Shifting remaining elements reclaims positions but costs work. A circular queue instead wraps indices to reuse the array while preserving the logical order.
For an array of capacity k, one clear model uses front as the next removal position, rear as the next insertion position and count as the number of items. Initially all are zero. Empty means count=0 and full means count=k. After insertion, rear=(rear+1) mod k and count increases.
After removal, front=(front+1) mod k and count decreases. In this count-based convention, front=rear can occur when empty or full, so indices alone do not distinguish them. Other implementations reserve a slot or use different rear meanings; do not combine their rules.
A priority queue removes the item with the most urgent specified priority rather than always the earliest arrival. Define whether smaller or larger priority numbers represent greater urgency. A tie-breaking rule, often arrival order, is also required for predictable behaviour.
Different representations move work between insertion and removal: an unsorted collection can insert cheaply but scan to remove the highest priority, while a heap supports efficient balanced costs. Choose from the required operations and scale rather than assuming every queue uses the same algorithm.
| Operation, capacity 4 | front | rear | count |
|---|---|---|---|
| Initial | 0 | 0 | 0 |
| Add A, B, C | 0 | 3 | 3 |
| Remove A, B | 2 | 3 | 1 |
| Add D, E | 2 | 1 | 3 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.