0
0
DSA Goprogramming~10 mins

Kth Smallest Element in BST in DSA Go - Interactive Practice

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

Complete 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'
Ainorder
Bpostorder
Cpreorder
Dtraverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using preorder or postorder traversal instead of inorder.
Forgetting to call the function recursively on the right child.
2fill in blank
medium

Complete 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'
Aresult[k]
Bresult[len(result)-k]
Cresult[k-1]
Dresult[0]
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.
3fill in blank
hard

Fix 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'
Ainorder
Bpostorder
Cpreorder
Dtraverse
Attempts:
3 left
💡 Hint
Common Mistakes
Passing slice by value causing no changes to original slice.
Using wrong traversal function in recursive call.
4fill in blank
hard

Fill 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'
A>
B<
Clen(word)
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Assigning word instead of its length in the map.
5fill in blank
hard

Fill 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'
A==
B>
Ck
Dstrings.ToUpper(k)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causing no filtering.
Using wrong variable for key transformation.