0
0
DSA C++programming~10 mins

Tree vs Array vs Linked List When Hierarchy Matters in DSA C++ - Interactive Comparison Practice

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

Complete the code to declare a simple array of integers.

DSA C++
int arr[5] = [1];
Drag options to blanks, or click blank then click option'
A[1, 2, 3, 4, 5]
B{1, 2, 3, 4, 5}
C(1, 2, 3, 4, 5)
D<1, 2, 3, 4, 5>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or square brackets instead of curly braces.
Forgetting to use any braces.
2fill in blank
medium

Complete the code to create a linked list node struct with an integer value and a pointer to the next node.

DSA C++
struct Node {
    int data;
    [1] next;
};
Drag options to blanks, or click blank then click option'
ANode&
Bint*
CNode*
DNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using reference & instead of pointer *.
Using int* instead of Node*.
3fill in blank
hard

Fix the error in the tree node constructor to initialize the value and set left and right children to nullptr.

DSA C++
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left([1]), right([2]) {}
};
Drag options to blanks, or click blank then click option'
ANULL
Bnullptr
C0
DNULLPTR
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Using 0 which is less clear.
4fill in blank
hard

Fill both blanks to create a function that returns the number of children of a binary tree node.

DSA C++
int countChildren(TreeNode* node) {
    int count = 0;
    if (node->left [1] nullptr) count++;
    if (node->right [2] nullptr) count++;
    return count;
}
Drag options to blanks, or click blank then click option'
A!=
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator which counts missing children.
Using greater or less than operators which are invalid for pointers.
5fill in blank
hard

Fill all three blanks to create a function that builds a linked list from an array of integers.

DSA C++
Node* buildList(int arr[], int size) {
    if (size == 0) return [1];
    Node* head = new Node();
    head->data = arr[0];
    head->next = buildList(arr + 1, [2]);
    return [3];
}
Drag options to blanks, or click blank then click option'
Anullptr
Bsize - 1
Chead
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of nullptr.
Passing wrong size in recursive call.
Returning wrong variable instead of head.