Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform an inorder traversal of the BST.
DSA Go
func inorder(root *TreeNode, result *[]int) {
if root == nil {
return
}
inorder(root.Left, result)
*result = append(*result, root.Val)
[1](root.Right, result)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using preorder or postorder traversal instead of inorder.
Forgetting to call the function recursively on the right child.
✗ Incorrect
The inorder traversal visits the left subtree, then the root, then the right subtree. So the recursive call on the right subtree must be inorder.
2fill in blank
mediumComplete the code to return the kth smallest element from the inorder traversal result.
DSA Go
func kthSmallest(root *TreeNode, k int) int {
result := []int{}
inorder(root, &result)
return [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using result[k] which is off by one.
Using result[len(result)-k] which gives kth largest, not smallest.
✗ Incorrect
The kth smallest element is at index k-1 because slice indices start at 0.
3fill in blank
hardFix the error in the recursive inorder traversal to avoid modifying the result slice incorrectly.
DSA Go
func inorder(root *TreeNode, result *[]int) {
if root == nil {
return
}
inorder(root.Left, result)
*result = append(*result, root.Val)
[1](root.Right, result)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing slice by value causing no changes to original slice.
Using wrong traversal function in recursive call.
✗ Incorrect
The function should use a pointer to the slice to modify the original slice. The recursive call must be inorder to continue traversal.
4fill in blank
hardFill both blanks to create a map of word lengths for words longer than 3 characters.
DSA Go
lengths := map[string]int{}
for _, word := range words {
if len(word) [1] 3 {
lengths[word] = [2]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Assigning word instead of its length in the map.
✗ Incorrect
We check if word length is greater than 3 and then store the length in the map.
5fill in blank
hardFill all three blanks to create a filtered map with uppercase keys and values greater than 0.
DSA Go
filtered := map[string]int{}
for k, v := range data {
if v [1] 0 {
filtered[[2]] = v
}
}
for key := range filtered {
newKey := [3]
filtered[newKey] = filtered[key]
delete(filtered, key)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causing no filtering.
Using wrong variable for key transformation.
✗ Incorrect
We filter values greater than 0, keep original keys for assignment, then create uppercase keys.