0
0
DSA Typescriptprogramming~10 mins

Binary Search on Answer Technique in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A0
B1
C-1
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Starting low at 1 or negative values without reason.
2fill in blank
medium

Complete 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'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
3fill in blank
hard

Fix 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'
Amid + 1
Bmid - 1
Cmid
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 or low incorrectly.
4fill in blank
hard

Fill 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'
A<=
B<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition or subtracting 1 from low incorrectly.
5fill in blank
hard

Fill 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'
AcanAchieve
B<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition or wrong function name calls.