0
0
DSA Typescriptprogramming~10 mins

Binary Search as Divide and Conquer 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 find the middle index in binary search.

DSA Typescript
const mid = Math.floor((low + [1]) / 2);
Drag options to blanks, or click blank then click option'
Ahigh
Bmid
Clow
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of high causes incorrect calculation.
Using low twice results in always the same index.
2fill in blank
medium

Complete the code to check if the middle element is the target.

DSA Typescript
if (arr[mid] === [1]) { return mid; }
Drag options to blanks, or click blank then click option'
Aarr
Bmid
Ctarget
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using low or arr instead of target.
3fill in blank
hard

Fix the error in updating the low index when target is greater than middle element.

DSA Typescript
if (arr[mid] < target) { low = [1]; }
Drag options to blanks, or click blank then click option'
Alow + 1
Bmid + 1
Cmid - 1
Dhigh - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1 causes infinite loop or wrong search.
Using high or low + 1 incorrectly.
4fill in blank
hard

Fill both blanks to update high index when target is less than middle element and to return -1 if not found.

DSA Typescript
if (arr[mid] > target) { [1] = mid - 1; } else if (low > high) { return [2]; }
Drag options to blanks, or click blank then click option'
Ahigh
B-1
Clow
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Updating low instead of high.
Returning 0 instead of -1 when not found.
5fill in blank
hard

Fill all three blanks to complete the recursive binary search function.

DSA Typescript
function binarySearch(arr: number[], target: number, low: number, high: number): number {
  if (low > high) return [1];
  const mid = Math.floor((low + [2]) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] < target) return binarySearch(arr, target, mid + 1, [3]);
  else return binarySearch(arr, target, low, mid - 1);
}
Drag options to blanks, or click blank then click option'
A-1
Bhigh
Clow
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 in base case.
Using low instead of high in mid calculation.
Passing wrong high value in recursive call.