0
0
DSA Goprogramming~20 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Go - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST vs Hash Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inorder Traversal on BST Insertions
Given the following sequence of insertions into an empty Binary Search Tree (BST), what is the output of an inorder traversal?
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}

func inorder(root *Node, res *[]int) {
    if root == nil {
        return
    }
    inorder(root.left, res)
    *res = append(*res, root.val)
    inorder(root.right, res)
}

func main() {
    var root *Node
    values := []int{50, 30, 70, 20, 40, 60, 80}
    for _, v := range values {
        root = insert(root, v)
    }
    var result []int
    inorder(root, &result)
    fmt.Println(result)
}
A[20, 40, 30, 60, 80, 70, 50]
B[20, 30, 40, 50, 60, 70, 80]
C[80, 70, 60, 50, 40, 30, 20]
D[50, 30, 20, 40, 70, 60, 80]
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns values in sorted order.
🧠 Conceptual
intermediate
1:30remaining
Why Use BST Over Hash Map for Ordered Data?
Which of the following is the main reason to choose a Binary Search Tree (BST) over a Hash Map when you need ordered data?
ABST maintains elements in sorted order allowing range queries and ordered traversal.
BBST provides faster average lookup time than Hash Map.
CBST uses less memory than Hash Map in all cases.
DBST automatically handles duplicate keys better than Hash Map.
Attempts:
2 left
💡 Hint
Think about what data structure allows you to get sorted data easily.
Predict Output
advanced
1:00remaining
Hash Map Key Existence Check Output
What is the output of the following Go code snippet that uses a map to check key existence?
DSA Go
package main

import "fmt"

func main() {
    m := map[int]string{1: "one", 2: "two", 3: "three"}
    val, ok := m[4]
    fmt.Println(val, ok)
}
Apanic: runtime error: invalid memory address or nil pointer dereference
B"four" true
C"zero" false
D"" false
Attempts:
2 left
💡 Hint
Accessing a non-existent key in a Go map returns zero value and false for existence.
🚀 Application
advanced
1:30remaining
Choosing Data Structure for Range Queries
You need to store a large set of integer keys and frequently perform queries to find all keys within a given range. Which data structure is best suited for this task?
ABinary Search Tree (BST) because it supports efficient range queries via inorder traversal.
BHash Map because it provides constant time lookup for any key.
CArray because it allows direct indexing of keys.
DStack because it supports last-in-first-out access.
Attempts:
2 left
💡 Hint
Think about which structure keeps keys sorted and allows scanning between two values.
🔧 Debug
expert
2:30remaining
Why Does This BST Insertion Cause Infinite Recursion?
Consider this Go code for inserting into a BST. Why does it cause infinite recursion?
DSA Go
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val <= root.val {
        insert(root.left, val)
    } else {
        insert(root.right, val)
    }
    return root
}
ABecause the function does not handle duplicate values correctly.
BBecause the base case is missing for nil root.
CBecause the recursive calls do not assign the returned node back to root.left or root.right.
DBecause the function returns root before recursion completes.
Attempts:
2 left
💡 Hint
Check if the recursive calls update the tree links properly.