← All Exercises

Priority Queue with container/heap

advancedgo~35min

Background

container/heap works with any type implementing heap.Interface; both Less and Swap must be consistent, and Update requires the element's current index.

Problem

Implement TaskQueue over []*Task with Less ordering by lower Priority then Name. Provide Push, Pop, and Update(task) error using heap.Fix, and demonstrate popping all tasks plus a priority update.

Input

Tasks with names and integer priorities, plus an optional priority change.

Output

Tasks popped in ascending priority with name as tie-breaker, reflecting the update.

Sample tests

Sample 1

Input: tasks: build/3, test/1, lint/2, docs/1
Expected: [docs, test, lint, build]

Sample 2

Input: build/3 updated to priority 0, with test/1
Expected: [build, test]

Your solution

Learning objective

Implement heap.Interface correctly and use heap.Fix for priority updates.