Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue with the root node and its index 0.
DSA Python
queue = [(root, [1])]Attempts:
3 left
💡 Hint
Common Mistakes
Starting index at 1 instead of 0 causes off-by-one errors.
Using the root node itself as index causes type errors.
✗ Incorrect
The root node is assigned index 0 to start the level indexing from zero.
2fill in blank
mediumComplete the code to update the maximum width after processing each level.
DSA Python
max_width = max(max_width, [1] - min_index + 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using min_index instead of max_index causes negative or zero width.
Forgetting to add 1 leads to off-by-one errors.
✗ Incorrect
Width is calculated as max_index - min_index + 1 for the current level.
3fill in blank
hardFix the error in the code that appends the left child with its correct index.
DSA Python
if node.left: queue.append((node.left, [1] * 2))
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' instead of 'index' causes type errors.
Using max_width or max_index instead of parent's index causes wrong indexing.
✗ Incorrect
The left child's index is 2 * parent's index, so we use 'index' here.
4fill in blank
hardFill both blanks to append the right child with its correct index and update the queue.
DSA Python
if node.right: queue.append((node.right, [1] * 2 + [2]))
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for the offset causes wrong indexing.
Using max_width instead of index causes errors.
✗ Incorrect
Right child's index is 2 * parent's index + 1, so blanks are 'index' and '1'.
5fill in blank
hardFill all three blanks to complete the while loop that processes each level and updates max_width.
DSA Python
while queue: level_length = len(queue) min_index = queue[0][1] max_index = queue[-1][[1]] for _ in range(level_length): node, index = queue.pop(0) if node.left: queue.append((node.left, index * 2 + [2])) if node.right: queue.append((node.right, index * 2 + [3])) max_width = max(max_width, max_index - min_index + 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong tuple index for max_index causes errors.
Swapping offsets for left and right children causes wrong indexing.
✗ Incorrect
max_index is at position 1 in tuple, left child index offset is 0, right child offset is 1.
