0
0
DSA Pythonprogramming~10 mins

Priority Queue Introduction and Concept in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an empty priority queue using Python's heapq module.

DSA Python
import heapq
pq = [1]
Drag options to blanks, or click blank then click option'
A[]
B{}
C()
Dset()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a dictionary or set instead of a list to store the priority queue.
2fill in blank
medium

Complete the code to add an element with priority 5 to the priority queue.

DSA Python
import heapq
pq = []
heapq.[1](pq, 5)
Drag options to blanks, or click blank then click option'
Apop
Binsert
Cheappush
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop or insert which are not heapq functions for adding elements.
3fill in blank
hard

Fix the error in the code to remove the smallest element from the priority queue.

DSA Python
import heapq
pq = [3, 5, 7]
heapq.heapify(pq)
smallest = heapq.[1](pq)
print(smallest)
Drag options to blanks, or click blank then click option'
Apopitem
Bpop
Cremove
Dheappop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes the last element, not the smallest.
4fill in blank
hard

Fill both blanks to create a priority queue from a list and then add a new element 2.

DSA Python
import heapq
pq = [4, 1, 7]
heapq.[1](pq)
heapq.[2](pq, 2)
Drag options to blanks, or click blank then click option'
Aheapify
Bheappush
Csorted
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using sorted instead of heapify to create a heap.
5fill in blank
hard

Fill all three blanks to pop the smallest element, then push 6, and finally peek at the smallest element without removing it.

DSA Python
import heapq
pq = [3, 8, 5]
heapq.heapify(pq)
smallest = heapq.[1](pq)
heapq.[2](pq, 6)
peek = pq[3]
Drag options to blanks, or click blank then click option'
Aheappop
Bheappush
C[0]
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to peek using pop() which removes elements.