Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array segment.
DSA C++
int mid = start + (end - start) / [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division by 3 or 4 instead of 2.
Forgetting to subtract start from end before dividing.
✗ Incorrect
The middle index is found by dividing the length by 2 to split the array into two halves.
2fill in blank
mediumComplete the code to create a new tree node with the middle element as its value.
DSA C++
TreeNode* node = new TreeNode([1][mid]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable name like 'list' or 'vector'.
Using 'tree' which is unrelated.
✗ Incorrect
The array is named 'arr', so we access the middle element using arr[mid].
3fill in blank
hardFix the error in the recursive call to build the left subtree.
DSA C++
node->left = buildBST(arr, start, [1] - 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start or end incorrectly as the end index.
Using mid + 1 which belongs to the right subtree.
✗ Incorrect
The left subtree includes elements before the middle, so end index is mid - 1.
4fill in blank
hardFill both blanks to build the right subtree correctly.
DSA C++
node->right = buildBST(arr, [1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start or mid - 1 as start index for right subtree.
Swapping start and end indices.
✗ Incorrect
The right subtree starts from mid + 1 to end of the array segment.
5fill in blank
hardFill all three blanks to complete the base case and recursive function call.
DSA C++
if (start > [1]) return nullptr; int mid = start + (end - start) / [2]; TreeNode* node = new TreeNode(arr[mid]); node->left = buildBST(arr, start, mid - 1); node->right = buildBST(arr, mid + 1, [3]); return node;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start instead of end in base case.
Dividing by wrong number in mid calculation.
Using wrong end index for right subtree.
✗ Incorrect
Base case checks if start is greater than end. Mid is calculated by dividing by 2. Right subtree ends at end.