Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the binary search with correct initial boundaries.
DSA Javascript
function search(nums, target) {
let left = 0;
let right = [1];
while (left <= right) {
// search logic
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting right to nums.length causes out-of-bound errors.
Using nums.length / 2 limits the search incorrectly.
✗ Incorrect
The right boundary should be the last index, which is length minus one.
2fill in blank
mediumComplete the code to calculate the middle index correctly.
DSA Javascript
while (left <= right) { let mid = (left [1] right) / 2; // rest of the code }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Multiplying left and right instead of adding.
✗ Incorrect
To find the middle, add left and right and divide by 2.
3fill in blank
hardFix the error in the condition to check if the left half is sorted.
DSA Javascript
if (nums[left] [1] nums[mid]) { // left half is sorted }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than instead of less than.
Using less than or equal instead of less than.
✗ Incorrect
If nums[left] is less than nums[mid], the left half is sorted.
4fill in blank
hardFill both blanks to update the search boundaries when target is in the left sorted half.
DSA Javascript
if (nums[left] <= target && target [1] nums[mid]) { right = [2]; } else { left = mid + 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than instead of less than for target comparison.
Setting right to left instead of mid.
✗ Incorrect
Target must be less than mid to be in left half, so right becomes mid.
5fill in blank
hardFill all three blanks to update boundaries when the right half is sorted and target is in it.
DSA Javascript
else { if (target [1] nums[mid] && target [2] nums[right]) { left = [3]; } else { right = mid - 1; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or equal instead of greater than for first comparison.
Setting left to mid instead of mid + 1.
✗ Incorrect
Target must be greater than mid and less or equal to right, so left moves to mid + 1.