Implement a Priority Queue
Problem Implement a priority queue from scratch — no library heap — supporting efficient insertion and extraction of the minimum (or maximum) element.
Input / Output
- Input: a sequence of operations —
insert(value),extractMin(),peek(), and optionallysize() - Output:
extractMin()returns and removes the smallest element currently held;peek()returns it without removing
Constraints
insertandextractMinmust run in O(log n);peekin O(1)- Backing store must be a single array/list — no node objects, no pointers
- Handle extraction from an empty queue explicitly (throw or return a sentinel)
Example
insert(5),insert(2),insert(8)→extractMin()returns2; a followingextractMin()returns5- Tricky case: duplicate priorities — a binary heap is not stable, so equal elements may emerge in any order. Break ties with an insertion counter if FIFO ordering matters.
asked …