Data Structures and Algorithms (DSA27) · Week 12

Heaps and Priority Queues (Lecture 12)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 22 · Answered: 0

  1. W12-M01 Recall

    In a min-heap, which statement is always true?

  2. W12-M02 Recall

    With 0-based indices, the left child of index i is at:

  3. W12-M03 Apply

    The parent of index 10 is at index:

  4. W12-M04 Apply

    The height of a heap holding 1,000 values (a single node has height 0) is:

  5. W12-M05 Apply

    Which array is a valid min-heap?

  6. W12-M06 Apply

    Q.push(2): how many swaps does the sift-up make?

  7. W12-M07 Apply

    Q.pop() returns 1. Which value is at the root afterwards?

  8. W12-M08 Explain

    Sift-down swaps an item with the better of its two children, not just any child that beats it, because:

  9. W12-M09 Recall

    heapify on an array of n values sifts down first at index:

  10. W12-M10 Recall

    The worst-case cost of heapify on n values is:

  11. W12-M11 Explain

    heapify costs less than n pushes because:

  12. W12-M12 Apply

    Pushing the values n, n – 1, …, 2, 1 (in that order) into an empty min-heap costs, in total:

  13. W12-M13 Recall

    peek() on a heap of n values costs:

  14. W12-M14 Recall

    Checking whether an arbitrary value x is in a heap of n values costs, in the worst case:

  15. W12-M15 Explain

    A heap is stored in an array, with no node objects, because:

  16. W12-M16 Apply

    enqueue("a", 2), enqueue("b", 1), enqueue("c", 1), then three dequeue()s return:

  17. W12-M17 Explain

    PriorityQueue pushes (priority, counter, item) rather than (priority, item) because:

  18. W12-M18 Apply

    The k largest of n values, found with a min-heap of at most k values, cost:

  19. W12-M19 Explain

    To find the k largest values you keep a min-heap of size k because:

  20. W12-M20 Recall

    The extra space of heap sort (Week 10) is:

  21. W12-M21 Explain

    Heap sort sorts into ascending order with a max-heap because:

  22. W12-M22 Apply

    After MaxHeap([3, 1, 4, 1, 5, 9, 2, 6]), the value at _items[1] is:

18 marks

Written questions

5 written questions (18 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 18

  1. W12-E1 Explain 4 marks

    Define a binary heap by its two properties. Explain why the shape property matters: what it gives the height, and what it gives the storage.

  2. W12-E2 Apply 4 marks

    Describe push and pop on a min-heap, including sift-up and sift-down, the cost of each, and the special cases the code must handle.

  3. W12-E3 Explain 4 marks

    Explain how heapify builds a heap bottom-up, why it must go from the last parent backwards, and show that it is O(n). Why are n pushes O(n log n) in the worst case?

  4. W12-E4 Explain 3 marks

    Compare four ways to implement a priority queue — unsorted array, sorted array, binary heap, balanced binary search tree — by the cost of enqueue, dequeue and peek. Which would you choose, and why?

  5. W12-E5 Explain 3 marks

    PriorityQueue stores (priority, counter, item). What goes wrong with (priority, item)? What does the counter add besides avoiding that error?