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 optionally size()
  • Output: extractMin() returns and removes the smallest element currently held; peek() returns it without removing

Constraints

  • insert and extractMin must run in O(log n); peek in 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() returns 2; a following extractMin() returns 5
  • 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 …
LeaderboardSalaryAccount