Skip to content

QuizNot attempted

Picking the data structure

You repeatedly need the smallest element of a changing collection (insert + remove-min). Which structure?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • A binary heap (priority queue): O(log n) insert and remove-min
  • A binary heap keeps the minimum at the root and restores order in O(log n) after an insert or a remove-min, which is exactly this workload (scheduling, Dijkstra, top-k, merging k lists).
  • Not sorted array: O(1) to read the min, but every insert shifts elements, O(n).
  • Not hash map: no ordering at all, finding the min is an O(n) scan.
  • Not linked list: unsorted means O(n) to find the min
  • kept sorted means O(n) to insert.