Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform an inorder traversal of the BST.
DSA Python
def inorder(node, result): if node is None: return inorder(node.left, result) result.append(node.val) [1](node.right, result)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name for the recursive call.
Forgetting to call the function on the right child.
✗ Incorrect
The inorder traversal visits the left child, then the node, then the right child. The recursive call to the right child is done by calling inorder(node.right, result).
2fill in blank
mediumComplete the code to return the kth smallest element from the inorder traversal list.
DSA Python
def kth_smallest(root, k): result = [] inorder(root, result) return result[[1]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k-1 as the index.
Using 0 or k+1 which are incorrect indices.
✗ Incorrect
List indices start at 0, so the kth smallest element is at index k-1 in the inorder list.
3fill in blank
hardFix the error in the recursive inorder traversal to avoid infinite recursion.
DSA Python
def inorder(node, result): if node is None: return inorder(node.left, result) result.append(node.val) inorder([1], result)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorder on node.left twice.
Calling inorder on the node itself or the result list.
✗ Incorrect
The inorder traversal must call itself on the right child to avoid infinite recursion and correctly traverse the tree.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each word to its length if the length is greater than 3.
DSA Python
lengths = {word: [1] for word in words if len(word) [2] 3}Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Mapping word to word instead of its length.
✗ Incorrect
The dictionary comprehension maps each word to its length using len(word), and filters words with length greater than 3 using >.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if the value is positive.
DSA Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using < or other operators instead of >.
Using k instead of v for values.
✗ Incorrect
The comprehension uses k.upper() as keys, v as values, and filters items where v > 0.
