Exam-style question
Try this first
A priority queue contains three jobs: Job A with priority 2, Job B with priority 5, and Job C with priority 1. The system removes one job and then adds Job D with priority 4. State the order in which the next three removals should occur, assuming a larger number means a higher priority. Explain your answer and include the relevant queue operations.
Model answer
What a good answer should say
- The first remove operation removes Job B because it has the highest priority, 5.
- Job D is then added with priority 4.
- The next remove operation removes Job D because its priority is higher than Job A's priority 2 and Job C's priority 1.
- The final remove operation removes Job A, followed by Job C if another removal is made.
Explanation
Why this works
A priority queue orders removal by priority rather than by the order in which items were added. The empty and full tests are still required to make removal and addition safe.
Common mistake
No common mistake is linked to this question yet.
