0
0
DSA Javascriptprogramming~10 mins

Why Binary Search and What Sorted Order Gives You in DSA Javascript - Test Your Knowledge

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

Complete the code to start binary search with the correct initial left index.

DSA Javascript
let left = [1];
Drag options to blanks, or click blank then click option'
A1
B0
C-1
Darray.length
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or negative index causes skipping the first element.
2fill in blank
medium

Complete the code to calculate the middle index correctly in binary search.

DSA Javascript
let mid = Math.floor((left + [1]) / 2);
Drag options to blanks, or click blank then click option'
Aright
Barray.length
Cleft
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using array.length instead of right causes wrong mid calculation.
3fill in blank
hard

Fix the error in updating the left index when the middle element is less than the target.

DSA Javascript
if (array[mid] < target) {
  left = [1];
}
Drag options to blanks, or click blank then click option'
Amid
Bright
Cleft + 1
Dmid + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting left to mid causes infinite loop if mid doesn't change.
4fill in blank
hard

Fill both blanks to update right index and return found index correctly.

DSA Javascript
if (array[mid] === target) {
  return [1];
} else if (array[mid] > target) {
  [2] = mid - 1;
}
Drag options to blanks, or click blank then click option'
Amid
Bleft
Cright
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Returning target instead of index; updating wrong boundary variable.
5fill in blank
hard

Fill all three blanks to complete the binary search function.

DSA Javascript
function binarySearch(array, target) {
  let left = 0;
  let right = array.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (array[mid] === target) {
      return [1];
    } else if (array[mid] < target) {
      left = [2];
    } else {
      right = [3];
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
Amid
Bmid + 1
Cmid - 1
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect boundary updates cause infinite loops or missed targets.