0
0
DSA Typescriptprogramming~10 mins

Binary Search Iterative Approach 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 middle index in the binary search loop.

DSA Typescript
let mid = Math.floor((low + [1]) / 2);
Drag options to blanks, or click blank then click option'
Ahigh
Blow
Cmid
Darray.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of high causes incorrect calculation.
Using array.length instead of high leads to wrong index.
2fill in blank
medium

Complete the code to update the low index when the target is greater than the middle element.

DSA Typescript
if (target > arr[mid]) {
  low = [1];
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bhigh
Clow + 1
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid causes infinite loop.
Setting low to high skips elements.
3fill in blank
hard

Fix the error in the loop condition to correctly continue searching while low is less than or equal to high.

DSA Typescript
while (low [1] high) {
  // binary search steps
}
Drag options to blanks, or click blank then click option'
A>
B!=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < misses checking when low equals high.
Using != can cause infinite loops.
4fill in blank
hard

Fill both blanks to correctly update the high index when the target is less than the middle element.

DSA Typescript
if (target < arr[mid]) {
  high = [1];
  low = [2];
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid + 1 skips elements.
Setting high to low causes errors.
5fill in blank
hard

Fill all three blanks to complete the binary search function that returns the index of the target or -1 if not found.

DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let low = 0;
  let high = arr.length - 1;
  while (low [1] high) {
    let mid = Math.floor((low + high) / 2);
    if (arr[mid] === target) {
      return [2];
    } else if (target < arr[mid]) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return [3];
}
Drag options to blanks, or click blank then click option'
A<=
Bmid
C-1
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < in loop misses last element.
Returning low or high instead of mid is wrong.
Returning 0 instead of -1 when not found.