Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the binary search boundaries.
DSA Javascript
let low = 0; let high = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to arr.length causes out-of-bound errors.
Setting high to 0 makes the search range empty.
✗ Incorrect
We set high to arr.length - 1 because array indices go from 0 to length minus one.
2fill in blank
mediumComplete the code to calculate the middle index safely.
DSA Javascript
let mid = low + Math.floor([1] / 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using (high + low) / 2 can cause overflow in some languages.
Using multiplication or division incorrectly changes the midpoint.
✗ Incorrect
We calculate mid as low + Math.floor((high - low) / 2) to avoid overflow and find the middle index.
3fill in blank
hardFix the error in the condition to move the low pointer.
DSA Javascript
if (canAchieve(mid)) { low = [1]; } else { high = mid - 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid causes infinite loops.
Setting low to mid - 1 moves backward incorrectly.
✗ Incorrect
When canAchieve(mid) is true, we move low to mid + 1 to search higher values.
4fill in blank
hardFill both blanks to complete the binary search loop condition and update.
DSA Javascript
while ([1] <= [2]) { let mid = low + Math.floor((high - low) / 2); if (canAchieve(mid)) { low = mid + 1; } else { high = mid - 1; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid in the condition causes wrong loop behavior.
Using low + 1 skips valid values.
✗ Incorrect
The loop runs while low <= high to cover all possible values in the search range.
5fill in blank
hardFill all three blanks to return the correct final answer after binary search.
DSA Javascript
return [1] - 1; // where [2] is the last successful mid // and [3] is the variable updated when canAchieve(mid) is true
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning high directly can be incorrect if low passed it.
Confusing which variable holds the last successful value.
✗ Incorrect
After the loop, low is one more than the last successful mid, so we return low - 1.