0
0
Data Structures Theoryknowledge~10 mins

Priority queue with heaps in Data Structures Theory - Interactive Code Practice

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

Complete the code to identify the root element in a max-heap.

Data Structures Theory
root = heap[[1]]
Drag options to blanks, or click blank then click option'
A1
B0
C-1
Dlen(heap)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the root index, which is incorrect for zero-based arrays.
Using -1 or len(heap), which do not point to the root.
2fill in blank
medium

Complete the formula to find the left child index of a node at index i in a heap.

Data Structures Theory
left_child_index = 2 * [1] + 1
Drag options to blanks, or click blank then click option'
A2
Bi + 1
Ci
Di - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to i before multiplying, which shifts the index incorrectly.
Using i - 1 or a fixed number instead of the node index.
3fill in blank
hard

Fix the error in the code to get the parent index of a node at index i in a heap.

Data Structures Theory
parent_index = ([1] - 1) // 2
Drag options to blanks, or click blank then click option'
Ai
Bi + 1
Ci - 1
D2 * i
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to i before subtracting, which gives wrong parent index.
Using multiplication instead of division.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each node to its depth in a heap of size n.

Data Structures Theory
{node: [1] for node in range(n) if node [2] n}
Drag options to blanks, or click blank then click option'
Anode.bit_length() - 1
B<
C>=
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '<' in the condition.
Using 'node' directly instead of calculating depth.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each node to its parent index in a heap of size n, excluding the root.

Data Structures Theory
{node: ([1] - 1) // 2 for node in range(1, [2]) if node [3] n}
Drag options to blanks, or click blank then click option'
Anode
Bn + 1
C<
Dnode + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node + 1' instead of 'node' for parent calculation.
Using '>=' instead of '<' in the condition.