0
0
DSA C++programming~20 mins

Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why Trees Are Used Instead of Arrays or Linked Lists

Which of the following is a key reason why trees are used instead of arrays or linked lists?

ATrees allow faster searching and hierarchical data representation.
BTrees use less memory than arrays and linked lists.
CTrees store data in a continuous block of memory.
DTrees do not require pointers or references.
Attempts:
2 left
💡 Hint

Think about how data is organized and searched in trees compared to arrays or linked lists.

Predict Output
intermediate
2:00remaining
Output of Tree Traversal vs Linked List Traversal

Given the following C++ code snippets, what is the output of the tree traversal compared to the linked list traversal?

DSA C++
struct Node {
    int val;
    Node* next;
    Node* left;
    Node* right;
};

// Linked list traversal
void printList(Node* head) {
    while (head) {
        std::cout << head->val << " ";
        head = head->next;
    }
}

// Inorder tree traversal
void inorder(Node* root) {
    if (!root) return;
    inorder(root->left);
    std::cout << root->val << " ";
    inorder(root->right);
}

int main() {
    // Linked list: 1 -> 2 -> 3
    Node* head = new Node{1, nullptr, nullptr, nullptr};
    head->next = new Node{2, nullptr, nullptr, nullptr};
    head->next->next = new Node{3, nullptr, nullptr, nullptr};

    // Tree:
    //     2
    //    / \
    //   1   3
    Node* root = new Node{2, nullptr, nullptr, nullptr};
    root->left = new Node{1, nullptr, nullptr, nullptr};
    root->right = new Node{3, nullptr, nullptr, nullptr};

    printList(head);
    std::cout << "\n";
    inorder(root);
    return 0;
}
A1 2 3\n3 2 1
B1 2 3\n2 1 3
C1 2 3\n1 2 3
D2 1 3\n1 2 3
Attempts:
2 left
💡 Hint

Recall how inorder traversal visits nodes in a binary search tree.

🔧 Debug
advanced
2:00remaining
Why Does This Tree Code Cause a Runtime Error?

Consider this C++ code snippet that tries to insert nodes into a binary tree. Why does it cause a runtime error?

DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
};

void insert(Node* root, int value) {
    if (value < root->val) {
        if (root->left == nullptr) {
            root->left = new Node{value, nullptr, nullptr};
        } else {
            insert(root->left, value);
        }
    } else {
        if (root->right == nullptr) {
            root->right = new Node{value, nullptr, nullptr};
        } else {
            insert(root->right, value);
        }
    }
}

int main() {
    Node* root = new Node{10, nullptr, nullptr};
    insert(root, 5);
    insert(root, 15);
    return 0;
}
AIt overwrites the root node value instead of inserting.
BIt tries to assign value to a null pointer without allocating memory.
CIt causes a syntax error due to missing semicolons.
DIt causes infinite recursion because base case is missing.
Attempts:
2 left
💡 Hint

Check what happens when root->left or root->right is nullptr before assignment.

📝 Syntax
advanced
2:00remaining
Which Option Correctly Defines a Tree Node in C++?

Which of the following C++ struct definitions correctly defines a binary tree node with integer values?

Astruct Node { int val; Node left*; Node right*; };
Bstruct Node { int val; Node left; Node right; };
Cstruct Node { int val; Node* left; Node right; };
Dstruct Node { int val; Node* left; Node* right; };
Attempts:
2 left
💡 Hint

Remember how pointers are declared in C++ structs.

🚀 Application
expert
2:00remaining
Why Can't Linked Lists Efficiently Represent Hierarchical Data?

Which of the following best explains why linked lists are not efficient for representing hierarchical data structures like file systems or organizational charts?

ALinked lists are linear and cannot represent multiple child nodes per parent efficiently.
BLinked lists require more memory than trees for the same data.
CLinked lists do not allow traversal of elements.
DLinked lists cannot store integer values.
Attempts:
2 left
💡 Hint

Think about how many children a node can have in a hierarchy and how linked lists store data.