Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the low boundary for binary search.
DSA Typescript
let low = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting low at 1 or negative values without reason.
✗ Incorrect
We start binary search from 0 as the lowest possible answer.
2fill in blank
mediumComplete the code to calculate the middle point in binary search.
DSA Typescript
let mid = (low [1] high) / 2;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
✗ Incorrect
We add low and high, then divide by 2 to find the middle.
3fill in blank
hardFix the error in the condition to update the high boundary.
DSA Typescript
if (canAchieve(mid)) { high = [1]; } else { low = mid + 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 or low incorrectly.
✗ Incorrect
If mid is achievable, we move high to mid to search lower values.
4fill in blank
hardFill both blanks to complete the binary search loop condition and update low.
DSA Typescript
while (low [1] high) { let mid = (low + high) / 2; if (canAchieve(mid)) { high = mid; } else { low = mid [2] 1; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition or subtracting 1 from low incorrectly.
✗ Incorrect
The loop runs while low is less than high, and low updates by adding 1.
5fill in blank
hardFill all three blanks to create a function that uses binary search on answer technique.
DSA Typescript
function binarySearchAnswer(low: number, high: number, [1]: (mid: number) => boolean): number { while (low [2] high) { let mid = Math.floor((low + high) / 2); if ([3](mid)) { high = mid; } else { low = mid + 1; } } return low; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition or wrong function name calls.
✗ Incorrect
The function takes a check function 'canAchieve', loops while low < high, and calls canAchieve(mid) inside.