Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array.
DSA Javascript
const mid = Math.[1]((start + end) / 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.ceil causes index to go out of range.
Using Math.round can pick wrong middle for even length arrays.
✗ Incorrect
We use Math.floor to get the middle index as an integer by rounding down.
2fill in blank
mediumComplete the code to create a new tree node with the middle element.
DSA Javascript
const node = new TreeNode([1][mid]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names like 'arr' or 'list'.
✗ Incorrect
The input array is named 'nums', so we use nums[mid] to get the middle element.
3fill in blank
hardFix the error in the recursive call to build the left subtree.
DSA Javascript
node.left = buildBST(nums, start, [1] - 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid + 1' causes overlap with right subtree.
Using 'end' or 'start' incorrectly limits the range.
✗ Incorrect
We use 'mid - 1' to build the left subtree from start to just before mid.
4fill in blank
hardFill both blanks to build the right subtree correctly.
DSA Javascript
node.right = buildBST(nums, [1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid - 1 as start index for right subtree.
Using start as end index causes wrong range.
✗ Incorrect
Right subtree starts from mid + 1 to end of the array segment.
5fill in blank
hardFill all three blanks to complete the main function that calls the helper.
DSA Javascript
function sortedArrayToBST([1]) { function buildBST([2], start, end) { if (start > end) return null; const mid = Math.floor((start + end) / 2); const node = new TreeNode(nums[mid]); node.left = buildBST(nums, start, mid - 1); node.right = buildBST(nums, mid + 1, end); return node; } return buildBST([3], 0, nums.length - 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names like 'array' causes reference errors.
✗ Incorrect
The array parameter and recursive calls use the name 'nums' consistently.