0
0
DSA Goprogramming~20 mins

Convert Sorted Array to Balanced BST in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Balanced BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Balanced BST Inorder Traversal
Given the sorted array [1, 2, 3, 4, 5, 6, 7], a balanced BST is created by picking the middle element as root recursively. What is the inorder traversal output of the resulting BST?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid])
    root.Right = sortedArrayToBST(nums[mid+1:])
    return root
}

func inorder(root *TreeNode) []int {
    if root == nil {
        return []int{}
    }
    left := inorder(root.Left)
    right := inorder(root.Right)
    return append(append(left, root.Val), right...)
}

func main() {
    nums := []int{1, 2, 3, 4, 5, 6, 7}
    root := sortedArrayToBST(nums)
    result := inorder(root)
    fmt.Println(result)
}
A[3, 2, 1, 4, 5, 6, 7]
B[1, 2, 3, 4, 5, 6, 7]
C[7, 6, 5, 4, 3, 2, 1]
D[4, 2, 1, 3, 6, 5, 7]
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the sorted array.
🧠 Conceptual
intermediate
1:30remaining
Height of Balanced BST from Sorted Array
If you convert a sorted array of length 15 into a balanced BST by always choosing the middle element as root, what is the height of the resulting BST?
A6
B3
C5
D4
Attempts:
2 left
💡 Hint
Height of a balanced BST is about log2(n+1).
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Construction
What error will this Go code produce when converting a sorted array to a balanced BST?
DSA Go
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid-1])
    root.Right = sortedArrayToBST(nums[mid+1:])
    return root
}
Aruntime error: slice bounds out of range
Bnil pointer dereference
Ccompilation error: invalid slice index
Dno error, runs correctly
Attempts:
2 left
💡 Hint
Check the slice indices carefully when slicing arrays.
🚀 Application
advanced
1:30remaining
Number of Nodes in Left Subtree
Given a sorted array of length 9, after converting it to a balanced BST by choosing the middle element as root, how many nodes are in the left subtree of the root?
A5
B3
C4
D8
Attempts:
2 left
💡 Hint
Left subtree contains elements before the middle index.
📝 Syntax
expert
2:30remaining
Correct Go Syntax for Balanced BST Construction
Which option correctly implements the balanced BST construction from a sorted array in Go without syntax errors?
A
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid])
    root.Right = sortedArrayToBST(nums[mid+1:])
    return root
}
B
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid-1])
    root.Right = sortedArrayToBST(nums[mid+1:])
    return root
}
C
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid])
    root.Right = sortedArrayToBST(nums[mid:])
    return root
}
D
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    mid := len(nums) / 2
    root := &TreeNode{Val: nums[mid]}
    root.Left = sortedArrayToBST(nums[:mid+1])
    root.Right = sortedArrayToBST(nums[mid+1:])
    return root
}
Attempts:
2 left
💡 Hint
Check slice indices carefully to avoid overlap or out of range.